0

How do I combine all the vectors in small subsets of vectors in Matlab?

a= [5 6 7] b = [8 9 10] c=[11 12 13] d=[14 15 16] e=[17 18 19]

a combine with b and c:

Outcome:

M1= [ 5 6 7 8 9 10 11 12 13]

a with b and d:

M2 = [5 6 7 8 9 10 14 15 16]

and so on .....

Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
Emanuel
  • 33
  • 6

3 Answers3

2

This answer covers the case for an arbitrary number of vectors. The vectors are assumed to be row vectors of equal length.

Let your example data be defined as

a = [5 6 7]; b = [8 9 10]; c = [11 12 13]; d = [14 15 16]; e = [17 18 19];
vectors = {a, b, c, d, e}; %// cell array with any number of row vectors of equal size
n = 3; %// desired subset size

Then: generate all combinations of indices, use that to index into vectors, concatenate into one big row vector, and reshape that to obtain the desired result:

combs = nchoosek(1:numel(vectors), n);
result = reshape([vectors{combs.'}], numel(vectors{1})*n, []).';

This gives a matrix whose first row is your M1, second row is M2 etc:

result =
     5     6     7     8     9    10    11    12    13
     5     6     7     8     9    10    14    15    16
     5     6     7     8     9    10    17    18    19
     5     6     7    11    12    13    14    15    16
     5     6     7    11    12    13    17    18    19
     5     6     7    14    15    16    17    18    19
     8     9    10    11    12    13    14    15    16
     8     9    10    11    12    13    17    18    19
     8     9    10    14    15    16    17    18    19
    11    12    13    14    15    16    17    18    19
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
  • Yes, as stated in my code. The OP example seems to suggest that. But the question is not clear – Luis Mendo Apr 16 '15 at 21:29
  • You don't need to deal with cell arrays then I guess: `A = cat(1,a,b,c,d,e).'; out = reshape(A(:,nchoosek(1:5,3).'),9,[]).';`? Very similar to your existing one. – Divakar Apr 16 '15 at 21:31
  • @Divakar Yes, for the 5-vector case I agree. I tried to make it more general so that it works for any number of vectors. I'll make that clearer in my answer – Luis Mendo Apr 16 '15 at 21:34
  • Luis Mendo's answer is what I was looking for. A general approach for same size vectors. Thank you very much. – Emanuel Apr 18 '15 at 06:09
1

You can use the cat function:

res = cat(2,a,b,c);

or simply the [] syntax:

res = [a b c];

In both cases, res will contain [5 6 7 8 9 10 11 12 13].

Best,

Ratbert
  • 5,463
  • 2
  • 18
  • 37
0

Use nchoosek.

a = [5 6 7]; b = [8 9]; c = [11 12 13]; d = [14 15 16]; e = [17 18 19];
N = 3;
x = {a,b,c,d,e};
y = nchoosek(x,N);

And you have all the combination of your arrays taken N at a time in a cell array. Each row i of the cell x is a combination, so to have it back as a row vector just do

horzcat(y{i,:})

Or, if you want to get them all and put them in a cell array of size n_combs

n_combs = size(y,1);
out = cell(0,n_combs);
for i = 1 : n_combs
    out{i} = horzcat(1, y{i,:});
end

There is no constraint on the size of the arrays that you want to combine, e.g., you can combine

a = [5 7]; b = [8 9]; c = [11]; d = [20 14 15 16]; e = [17 18 19];

However, if you must put together all the combination in a matrix, then the arrays have to be of the same size. In this case Luis Mendo's answer does the job.

Finally, if repetitions are allowed use nmultichoosek instead of nchoosek.

Community
  • 1
  • 1
Simon
  • 5,070
  • 5
  • 33
  • 59