How to reshape matrix without loop?
1 view (last 30 days)
Show older comments
Want to rearrange the matrix A(a,b,c,d) to B(b,c1,c2,d,a), where a,b,c,d are dimensions, c=c1*c2. If known c1=3, for example, B should be:
Can it be done more elegantly and without the loop?
John D'Errico on 26 Jul 2017
WTP? Effectively two lines, one more to get the size of A if you don't know it already.
[a,b,c,d] = size(A);
B = reshape(A,[a,b,c1,c/c1,d]);
B = permute(B,[2 3 4 5 1]);
There is no need to use fix, because if c is not exactly divisible by c1, then A won't have the correct number of elements for a reshape anyway.
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!