How do I get every combination of 3 vectors?

조회 수: 3 (최근 30일)
Kevin Shen
Kevin Shen 2020년 8월 2일
댓글: Image Analyst 2020년 8월 2일
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