How to reshape matrix 2 columns by 2 columns

9 views (last 30 days)
Ren Li
Ren Li on 30 Jul 2020
Answered: Bruno Luong on 2 Aug 2020
for example, an array like this:
1 0 2 0 2 3
5 4 4 0 1 4
4 2 2 4 2 2
1 1 0 4 3 1
How to reshape it into an array like this:
1 0
5 4
4 2
1 1
2 0
4 0
2 4
0 4
2 3
1 4
2 2
3 1

Accepted Answer

Sriram Tadavarty
Sriram Tadavarty on 30 Jul 2020
Hi Ren,
Use individual columns and append them. For the sample code you provided, it can be done as below:
a = [1 0 2 0 2 3;
5 4 4 0 1 4;
4 2 2 4 2 2;
1 1 0 4 3 1];
a1 = a(:,1:2:end); % Select the odd numbered columns
a2 = a(:,2:2:end); % Select the even numbered columns
[a1(:) a2(:)] % Append them
Hope this helps.
Regards,
Sriram
  4 Comments
madhan ravi
madhan ravi on 30 Jul 2020
No problem Sriram ;). By the way I think concatenate could be used instead of append I believe. Append is something which is used for adding something at the end of already existing variable/array.

Sign in to comment.

More Answers (3)

Walter Roberson
Walter Roberson on 30 Jul 2020
cell2mat(mat2cell(YourArray, size(YourArray,1), 2*ones(1,size(YourArray,2)/2)).')
  1 Comment
Ren Li
Ren Li on 2 Aug 2020
Thank you very much for the code, after spending sometime understanding it, I believe this gives even more flexibility on how to rearrange the matrix, now with this code, I can easily reshape it N columns by N columns, which in my case is exactly what I will need to do next. So again thank you so much

Sign in to comment.


madhan ravi
madhan ravi on 30 Jul 2020
First = matrix(:, 1:2:end);
Second = matrix(:, 2:2:end);
Wanted = [First(:), Second(:)]

Bruno Luong
Bruno Luong on 2 Aug 2020
You want a reshape? Here is reshape method:
B = reshape(permute(reshape(A,size(A,1),2,[]),[1 3 2]),[],2)

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!