In my script I generate a matrix where each column is coupled with at least another one. For example, 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.
This is for the moment just an image in mind, but then I would like to move coupled columns on their own line, so that I can easily mix them up using any() or sum().
This will become clearer with this example:
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:
ans =
1 4
2 5
3 6
7 10
8 11
9 12
As I said, this example is just for 2 columns, but in my use case I also need to support any number of columns couples. 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 that in a vectorized manner?
Assuming
M
to be the input matrix, see if this works for you -Sample runs -
and
Covering another possible intended question
Going by your words -
"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"
, I am sensing you might actually be looking to form all possible combinations of the columns of the input matrix and vertically concatenate them to form a slenderish matrix as the output. This section of the solution would cover that base. The code to achieve such a goal (if that's what you meant hopefully) would be something like this -One sample run -