Rearrange column blocks below previous tiles

In my script, I create a matrix in which each column is associated with at least one. For example, column 1 is associated with column 2, column 3 is associated with column 4, etc. But I could also associate 3 by 3 or 4 by 4 columns or any other number.

At the moment this is just an image in my mind, but then I would like to move the related columns to my line so that I can easily mix them using either () or sum ().

In this example, this will become clearer:

A = reshape(1:12, 3, []) % A is the matrix I start with, this reshape is OK
A =

    1    4    7   10
    2    5    8   11
    3    6    9   12

reshape(A, [], 2) % this reshape is not OK
ans =

    1    7
    2    8
    3    9
    4   10
    5   11
    6   12

However, I would like the answer to be as follows:

ans =

    1    4
    2    5
    3    6
    7   10
    8   11
    9   12

As I said, this example is only for two columns, but in my use case I also need to support any number of pairs of columns. Here for 3 columns:

B = reshape(1:18, 3, [])
B =

    1    4    7   10   13   16
    2    5    8   11   14   17
    3    6    9   12   15   18

reshape(B, [], 3)
ans =

    1    7   13
    2    8   14
    3    9   15
    4   10   16
    5   11   17
    6   12   18

What I would like:

ans =

    1    4   7
    2    5   8
    3    6   9
   10   13  16
   11   14  17
   12   15  18

Is there any way to do this in vector form?

+4
2

, M , , -

ncols = 2; %// number of columns (needs to be edited)
[m,n] = size(M) %// get size of input matrix for later usage
r = numel(M)/(m*ncols);
out = reshape(permute(reshape(M,m,ncols,[]),[1 3 2]),m*r,[])

-

M =
     1     4     7    10
     2     5     8    11
     3     6     9    12
ncols =
     2
out =
     1     4
     2     5
     3     6
     7    10
     8    11
     9    12

M =
     1     4     7    10    13    16
     2     5     8    11    14    17
     3     6     9    12    15    18
ncols =
     3
out =
     1     4     7
     2     5     8
     3     6     9
    10    13    16
    11    14    17
    12    15    18

- "column 1 is coupled with column 2, column 3 is coupled with column 4, etc... But I could also couple columns 3 by 3 or 4 by 4 or any other number", , , . . ( , , ) - -

ncols = 2; %// number of columns (needs to be edited)
[m,n] = size(M) %// get size of input matrix for later usage
combs = dec2base(0:n^2-1,n,ncols)-'0'+1 %// find combinations
combsp = permute(combs,[3 2 1]) %// make a 3D array of those combinations

idx = bsxfun(@plus,[1:m]',(combsp-1)*m) %//'# Indices as a 3D array
idx1 = reshape(permute(idx,[1 3 2]),m*size(idx,3),[]) %// vertically concatenate 
                                            %// 3D indices array into a 2D array
out = M(idx1) %// desired output

-

M =
     6     7     3     6
     3     1     6     3
     5     1     4     2

ncols = 2

out =
     6     6
     3     3
     5     5
     6     7
     3     1
     5     1
     6     3
     3     6
     5     4
     6     6
     3     3 ....    
+4

Divakar , Octave, Octave N-D , , :

-, N- , Octave ; . , SparseMatrix Octaves , NDArray.

: GNU Octave

, :

function B = vertical_tile(A, ncols)

B = [];
if issparse(A)
    B = sparse(B);
end

for i=1:ncols
    B = [B A(:, i:ncols:end)];
end
B = reshape(B, [], ncols);

end

:

vertical_tile(A, 2)
ans =

    1    4
    2    5
    3    6
    7   10
    8   11
    9   12

vertical_tile(B, 3)
ans =

    1    4    7
    2    5    8
    3    6    9
   10   13   16
   11   14   17
   12   15   18
+1

All Articles