How to reshape any matrix using while loop or any other method?
5 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Sushil Pokharel
il 29 Giu 2022
Modificato: Sushil Pokharel
il 30 Giu 2022
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 Commenti
Risposta accettata
Voss
il 29 Giu 2022
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]))
Più risposte (1)
Walter Roberson
il 29 Giu 2022
A = B(:, 1:6:end) ;
5 Commenti
Voss
il 29 Giu 2022
Modificato: Voss
il 29 Giu 2022
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), [])
Vedere anche
Categorie
Scopri di più su Matrices and Arrays in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!