This is easy using linear indexing (a loop is not required):
>> [M(1:10:end);M(2:11:end);M(3:12:end);M(4:13:end)]
ans =
10 20 30
11 22 33
12 24 36
13 26 39
Note that M has 10 rows, so you can easily adjust the code I gave to work for any number of rows:
>> S = size(M,1);
>> [M(1:S+0:end);M(2:S+1:end);M(3:S+2:end);M(4:S+3:end)]
ans =
10 20 30
11 22 33
12 24 36
13 26 39
Note that this method only works if the number of columns is <= the number of rows.
To adjust for N elements from each column you can add a loop, either following the method above:
N = 4;
Y = nan(N,size(M,2));
S = size(M,1);
for k = 1:N
Y(k,:) = M(k:S+k-1:end);
end
or doing a naive implementation of what you explained in your question.
Best Answer