How to reshape any matrix using while loop or any other method?
6 次查看(过去 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 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!