How do I get every combination of 3 vectors?

3 vues (au cours des 30 derniers jours)
Kevin Shen
Kevin Shen le 2 Août 2020
Commenté : Image Analyst le 2 Août 2020
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

Réponse acceptée

Bruno Luong
Bruno Luong le 2 Août 2020
Modifié(e) : Bruno Luong le 2 Août 2020
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 commentaires
Bruno Luong
Bruno Luong le 2 Août 2020
Modifié(e) : Bruno Luong le 2 Août 2020
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 le 2 Août 2020
Thanks for the help, and yes I definately do need more learning, I'm just getting started.

Connectez-vous pour commenter.

Plus de réponses (2)

Matt J
Matt J le 2 Août 2020
Example:
[X,Y,Z]=ndgrid(1:3,10:15,100:102);
[X(:),Y(:),Z(:)]
  5 commentaires
Matt J
Matt J le 2 Août 2020
Modifié(e) : Matt J le 2 Août 2020
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 le 2 Août 2020
Modifié(e) : Kevin Shen le 2 Août 2020
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.

Connectez-vous pour commenter.


Image Analyst
Image Analyst le 2 Août 2020
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 commentaires
Kevin Shen
Kevin Shen le 2 Août 2020
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 le 2 Août 2020
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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by