Convert permute and reshape to for
Afficher commentaires plus anciens
Hello everybody,
I am trying to find an alternative to the last 3 lines of code:
N=20; M=30; O=50; P=10;
all_1 = N*O
all_2 = M*P
Matrix1 = reshape([1:all_1*all_2],[all_1,all_2])
Matrix2 = reshape(Matrix1,[N,all_1/N,M,all_2/M])
Matrix2 = permute(Matrix2,[1 4 3 2])
Matrix2 = reshape(Matrix2,[N*all_2/M, M*all_1/N])
Maybe I can use some loops, because I want to compare the efficency of two different methods.
Thanks
1 commentaire
John D'Errico
le 12 Oct 2020
You want to write a loop for that instead? Why in the name of god would you? Instead of nice efficient code that uses very little time, you want to write complicated loops to do the same thing?
You should see that a reshape requires very little time, since nothing in memory changes. All MATLAB needs to do is change an internal flag on the variable that indicate the shape. The permute takes a little more time, but still relatively little, because it is entirely written using fast compiled code. Loops would be a terrible way to achieve these things.
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!