How to reshape any matrix using while loop or any other method?
1 回表示 (過去 30 日間)
古いコメントを表示
Hello there, I have a matrix B of size 432000x120 and I want another matrix A of same size in such a way that:
A(: , 1) = B(: , 1)
A(: , 2) = B(: , 7)
A(: , 3) = B(: , 13) and so on
I have done by using two for loops but I wanted to solve this problem by other method (may be by using while loop or any other efficient method). You help will be greatly appreciated.
0 件のコメント
採用された回答
Voss
2022 年 6 月 29 日
Here's one way, based on the assumption that it goes
A(:,[1,2,3,...,20,21,22,...]) = B(:,[1,7,13,...,115,2,8,...])
% 5x120 matrix B:
B = reshape(1:5*120,120,[]).'
n = 6;
% construct A by reordering the columns of B:
A = B(:,(1:n)+(0:n:size(B,2)-1).')
% another way to do the same reordering:
A = B(:,reshape(1:size(B,2),n,[]).')
% check first and last column of each 20-column sequence:
isequal( ...
A(:,[1 20 21 40 41 60 61 80 81 100 101 120]), ...
B(:,[1 115 2 116 3 117 4 118 5 119 6 120]))
その他の回答 (1 件)
Walter Roberson
2022 年 6 月 29 日
A = B(:, 1:6:end) ;
5 件のコメント
Voss
2022 年 6 月 29 日
編集済み: Voss
2022 年 6 月 29 日
I don't think that works correctly when the number of columns is not 6^2:
B = (1:120) + (1:5).'*100;
A = reshape(permute(reshape(B, size(B,1), [], 6), [1 3 2]), size(B,1), [])
I guess it should be:
A = reshape(permute(reshape(B, size(B,1), [], size(B,2)/6), [1 3 2]), size(B,1), [])
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!