How do I get every combination of 3 vectors?

1 次查看(过去 30 天)
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
Bruno Luong 2020-8-2
编辑:Bruno Luong 2020-8-2
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)
  3 个评论
Bruno Luong
Bruno Luong 2020-8-2
编辑:Bruno Luong 2020-8-2
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
Kevin Shen 2020-8-2
Thanks for the help, and yes I definately do need more learning, I'm just getting started.

请先登录,再进行评论。

更多回答(2 个)

Matt J
Matt J 2020-8-2
Example:
[X,Y,Z]=ndgrid(1:3,10:15,100:102);
[X(:),Y(:),Z(:)]
  5 个评论
Matt J
Matt J 2020-8-2
编辑:Matt J 2020-8-2
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
Kevin Shen 2020-8-2
编辑:Kevin Shen 2020-8-2
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
Image Analyst 2020-8-2
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.
  6 个评论
Kevin Shen
Kevin Shen 2020-8-2
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
Image Analyst 2020-8-2
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by