# How to rearrange columns in a MATRIX?

23 views (last 30 days)

Show older comments

Leeba Ann Chacko
on 4 Sep 2022

Moved: Walter Roberson
on 5 Sep 2022

I have a 2x4 matrix 'X' which looks like the follwing

X = 17.0600 29.6780 23.0660 29.4160

19.0280 42.3270 22.7940 43.0240

I would like the last 2 columns to come below the 1st 2 column so that it will look like this

X= 17.0600 29.6780

19.0280 42.3270

23.0660 29.4160

22.7940 43.0240

How do I go about this using he reshape function?

### Accepted Answer

Dyuman Joshi
on 4 Sep 2022

Moved: Walter Roberson
on 5 Sep 2022

x=[1 2 4 4 5 6; 3 4 6 6 7 8; 3 4 5 6 6 7;4 5 6 8 7 8];

y=[reshape(x(:,1:2:end),[],1) reshape(x(:,2:2:end),[],1)]

### More Answers (2)

Stephen23
on 4 Sep 2022

x = [1,2,4,4,5,6; 3,4,6,6,7,8; 3,4,5,6,6,7; 4,5,6,8,7,8]

y = reshape(permute(reshape(x,size(x,1),2,[]),[1,3,2]),[],2)

James Tursa
on 4 Sep 2022

One way:

[X(:,1:2);X(:,3:4)]

##### 2 Comments

James Tursa
on 4 Sep 2022

Edited: James Tursa
on 4 Sep 2022

### See Also

### Categories

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!