# How do I get every combination of 3 vectors?

55 views (last 30 days)
Kevin Shen on 2 Aug 2020 at 0:05
Commented: Image Analyst on 2 Aug 2020 at 20:47
Hello!
I have 3 vectors, one is a 35x1, one is a 31x1 and the last one is a 13x1. I want to create some kind of loop that outputs a number from the first, a number from the second, and a number from a third, over and over, eventually outputting every combination.
If I am not clear, please comment.
Thanks!
-Kevin

Bruno Luong on 2 Aug 2020 at 5:54
Edited: Bruno Luong on 2 Aug 2020 at 6:08
yourvector1 = ...
yourvector2 = ...
yourvector3 = ...
Then
c = {yourvector1, yourvector2, yourvector3};
c = flip(c);
[c{:}] = ndgrid(c{:});
c = cellfun(@(x) x(:), flip(c), 'unif', 0);
c = cat(2, c{:});
check
disp(c)

Kevin Shen on 2 Aug 2020 at 6:10
Thanks! I will try this. Just a quick question, what is c, and if it is a matrix how would I separate individual parts to use one at a time?
Bruno Luong on 2 Aug 2020 at 6:28
c is an array of combination.
• Row dimension corresponds to different combination,
• Column dimension corresponds to yourvector1, yourvector2, yourvector3 values.
You have to tell us what you meant by "use one at the time", but it probably goes like this
for i=1:size(c,1)
combi = c(i,:); % 1 x 3 vector
% do something with it
...
end
You probably need more learning of basic stuff of MATLAB/programming.
Kevin Shen on 2 Aug 2020 at 16:40
Thanks for the help, and yes I definately do need more learning, I'm just getting started.

Matt J on 2 Aug 2020 at 0:10
Example:
[X,Y,Z]=ndgrid(1:3,10:15,100:102);
[X(:),Y(:),Z(:)]

Kevin Shen on 2 Aug 2020 at 4:50
Well I don't know how
Matt J on 2 Aug 2020 at 10:52
I mean did you run the code I gave you and did you inspect the result? You will see that every row of the output matrix is a combination of the inputs to ndgrid.
Kevin Shen on 2 Aug 2020 at 17:29
So it does work, thanks! Although it isn't the perfect solution for my current project, I will definately use this in another project I am working on for the sheer simplicity of it.

Image Analyst on 2 Aug 2020 at 0:38
Here's a for loop way to do it:
% Create 3 sample vectors, one is a 35x1, one is a 31x1 and the last one is a 13x1.
v1 = (1:35)'; % Actual values are whatever yours are - not a ramp like this.
v2 = (1:31)';
v3 = (1:13)';
% Get 3 random selection orderings:
order1 = randperm(length(v1));
order2 = randperm(length(v2));
order3 = randperm(length(v3));
% Now make 3 loops
for k1 = 1 : length(order1)
index1 = order1(k1);
for k2 = 1 : length(order2)
index2 = order2(k2);
for k3 = 1 : length(order3)
index3 = order3(k3);
fprintf('Processing index %d from v1, index %d from v2, and index %d from v3.\n', index1, index2, index3);
end
end
end
Of course you can do it with a built-in function rather than loops like Matt showed you.

Image Analyst on 2 Aug 2020 at 14:01
It absolutely gets every combination. Every single element of a vector gets paired with every single other element of the other vectors at some point (one of the iterations). Do you think some might get skipped? If so, why? Every element is in the order vectors and every one of them will get used. No combination will get skipped.
Kevin Shen on 2 Aug 2020 at 17:34
Thank you so much, this does work! However I could only accept one answer, and the other one was the one that applied to me more directly.
I deeply appreciate your hard work helping me
-Kevin
Image Analyst on 2 Aug 2020 at 20:47
You're welcome. Thanks for voting for it though. Matt's answer is the more MATLAB-y, vectorized way of doing it and is probably what I would have done. I just offered the looping way of doing it because that's what you specifically asked for, and sometimes the vectorized methods use functions, which, honestly, can be more confusing to beginners than a simple, intuitive for loop.