Using ALLCOMB for unknown number of vectors

osman (view profile)

on 18 Sep 2019 at 12:41
Latest activity Commented on by Jos (10584)

Jos (10584) (view profile)

on 19 Sep 2019 at 20:22
Hello There!
I have N number of vectors recorded into workspace. I need to perform combination among themselves by using allcomb. How could I introduce the vectors into the command (allcomb) in the form of B = ALLCOMB(A1,A2,A3,...,AN).

on 18 Sep 2019 at 12:46
How are your n vectors stored? If they are each in their own variable then what is wrong with just literally adding them all as input arguments as in the function signature?
osman

osman (view profile)

on 18 Sep 2019 at 13:09
My variables are dynamic. the number of variables is continuously changing. I can not write them as you said. I have to define a set of variable like B = ALLCOMB(A1,A2,A3,...,AN). I do not know how to do this.
John D'Errico

John D'Errico (view profile)

on 18 Sep 2019 at 13:20
READ THE RESPONSE BY BRUNO. Learn how to use cell arrays instead of dynamically naming your variables. Numbered variables is a terrible programming style, that if you continue to follow it, will continuously lead you into problems, just as you are having now.

Bruno Luong (view profile)

on 18 Sep 2019 at 12:47

Create a CELL array, and get rid of the method of naming your variables with number 1,2,....
C = {A1, A2, A3, ..., AN}
B = allcomb(C{:});

Bruno Luong

Bruno Luong (view profile)

on 19 Sep 2019 at 10:12
Do you understand when I wrote:
"They compule two different things."
% Example
U=bsxfun(@plus,(1:2),[0:2:6]')
% Your's
Your1 = allcomb(U(1,:),U(2,:),U(3,:),U(4,:))
C=cell(size(U,1),1);
for k = 1:size(U,1)
C{k}=eval('U(k,:)');
end
Your2 = allcomb(C{:})
% Jos's
C = mat2cell(U, size(U,1), ones(1,size(U,2)));
Jos = allcomb(C{:})
% Bruno's
C = mat2cell(U, ones(1,size(U,1)), size(U,2));
Bruno = allcomb(C{:})
osman

osman (view profile)

on 19 Sep 2019 at 10:23
Thanks so much! I really appreciate about what you made me recognize.
Jos (10584)

Jos (10584) (view profile)

on 19 Sep 2019 at 20:22
Thanks Bruno, for correcting me, and thereby helping osman :-)