How to reshape any matrix using while loop or any other method?
Show older comments
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.
Accepted Answer
More Answers (1)
Walter Roberson
on 29 Jun 2022
A = B(:, 1:6:end) ;
5 Comments
Sushil Pokharel
on 29 Jun 2022
Walter Roberson
on 29 Jun 2022
If you continue your sequence, you have A(:,20) = B(:,114) . What is to go into A(:,21) ?
%sample data
B = (1:36) + (1:5).'*100;
B
%the computation you need
A = reshape(permute(reshape(B, size(B,1), [], 6), [1 3 2]), size(B,1), []);
%display result
A
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), [])
Sushil Pokharel
on 30 Jun 2022
Edited: Sushil Pokharel
on 30 Jun 2022
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!