Compute all combinations of given vectors into a matrix

Hi all,
I have 4 vectors:
a=[1:4]'
b=[5:10]'
c=[11:15]'
d=[16:20]'
I want to calculate all 2-combinations like this:
1) append the second vector to the first one (e.g.: A=[a;b], B=[a;c], etc...);
2) calculate all the comcinations of the second vector appended to the first one;
3) put everything into a matrix: C=[A,B,etc...]
I don't want to make combinations of all the elements of the vectors, I just want to append the vectors as they are and combine them.
Anyone having an idea?
Thanks,
Matt

1 Commento

The question is not clear. For 4 vectors the job seems to be trivial and you can do this manually. I do not understand the point 2) and 3).

Accedi per commentare.

Risposte (2)

A = [ {1:4} ; {5:10} ; {11:15} ;{16:20}] ;
idx = 1:length(A) ;
%%append the second vector to the first one (e.g.: A=[a;b], B=[a;c], etc...);
id = randperm(3)+1 ;
A1 = cell(3,1) ;
for i = 1:length(id)
A1{i} = [A{1} A{id(i)}] ;
end
%%put everything into a matrix: C=[A,B,etc...]
id = perms(idx) ;
A2 = cell(size(id,1),1) ;
for i = 1:size(id,1)
A2{i} = [A{id(i,:)}] ;
end
Your question is not clear at all. Assuming you want to generate all combinations {[a;b], [a;c], [a;d], [b;c], [b;d], [c;d]}, you could just generate them manually since there's only 6. A generic method would be:
a=[1:4]'
b=[5:10]'
c=[11:15]'
d=[16:20]'
allvectors = {a, b, c, d};
combs = nchoosek(1:numel(allvectors), 2);
result = cell(1, size(combs, 1));
for row = 1:size(combs, 1)
result{row} = vertcat(allvectors{combs(row, :)});
end
Note that because of the different length of your input vectors, it's not possible to make a matrix out of result.

Categorie

Prodotti

Richiesto:

il 7 Mag 2017

Risposto:

il 8 Mag 2017

Community Treasure Hunt

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

Start Hunting!

Translated by