Relocating entire blocks of matrix without using loop

Without using a "for" loop I need to relocate entire blocks of a matrix in the following manner:
This means vertically rearranging blocks that are placed horizontally next to each other, thereby creating a matrix with new dimensions, but the same number of elements.

 Accepted Answer

M = [M(:,1:n);M(:,n+1:2*n);M(:,2*n+1:3*n)];

4 Comments

Thanks Roger. Actually, I guess putting 3 as the number of times the blocks are repeated was a bit misleading in my presentation. What I meant was for the number of blocks to also be a deliberate value. Is there a way to relocate them now?
I thought you might make that change. Here is a general method:
M = reshape(permute(reshape(M,m,n,i),[1,3,2]),m*i,n);
Note: It is not a good idea to use 'i' as the name of your index, because it can be confused with matlab's reserved notation for the complex square root of -1.
Awesome, exactly what I was looking for. Thanks!

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 28 May 2016

Commented:

on 29 May 2016

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!