How to reshape matrix 2 columns by 2 columns

4 ビュー (過去 30 日間)
Ren Li
Ren Li 2020 年 7 月 30 日
回答済み: Bruno Luong 2020 年 8 月 2 日
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

採用された回答

Sriram Tadavarty
Sriram Tadavarty 2020 年 7 月 30 日
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 件のコメント
Sriram Tadavarty
Sriram Tadavarty 2020 年 7 月 30 日
Hi Madhan,
I wasn't aware that there was another answer provided by the time i answered it. Looking at your answer now, yes, both are equivalent. Mine has some additional comments.
If i knew that this is already answered, i wouldn't have answered it at all. I think we both looked at the same time, but my answer publish went late by couple of mins.
Sorry for any trouble here. I didn't do anything intentional. I am even ok, if you wanted yours to be an accepted answer, as you mentioned early enough. My answer has just some additional comments.
Thanking you.
Regards,
Sriram
madhan ravi
madhan ravi 2020 年 7 月 30 日
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.

サインインしてコメントする。

その他の回答 (3 件)

Walter Roberson
Walter Roberson 2020 年 7 月 30 日
cell2mat(mat2cell(YourArray, size(YourArray,1), 2*ones(1,size(YourArray,2)/2)).')
  1 件のコメント
Ren Li
Ren Li 2020 年 8 月 2 日
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

サインインしてコメントする。


madhan ravi
madhan ravi 2020 年 7 月 30 日
First = matrix(:, 1:2:end);
Second = matrix(:, 2:2:end);
Wanted = [First(:), Second(:)]
  1 件のコメント
Ren Li
Ren Li 2020 年 7 月 30 日
thank you very much

サインインしてコメントする。


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

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by