How to get all possible combinations of three independent data sets?
7 views (last 30 days)
Show older comments
Aris van Houten
on 14 Apr 2021
Commented: Aris van Houten
on 14 Apr 2021
Hello All,
I have the following problem:
I have three independent data sets of x- and y- coordinates, let them be as follows for now:
Data set 1:
A = [A_x A_y];
B = [B_x B_y];
C = [C_x C_y];
Data set 2:
D= [D_x D_y];
E = [E_x E_y];
F = [F_x F_y];
G = [G_x G_y];
Data set 3:
H = [H_x H_y];
I = [I_x I_y];
J = [J_x J_y];
I want to generate all possible combinations with a length of 3, but there can only be one for each data set:
For example:
ADJ is ok (consits of three points in three different data sets)
BEG is not ok (because E and G are in data set 2)
P.S. The real data sets consists of 30+ coordinates
Thanks you very much in advance!
0 Comments
Accepted Answer
Walter Roberson
on 14 Apr 2021
A = [A_x A_y];
B = [B_x B_y];
C = [C_x C_y];
ABC = [A;B;C];
D= [D_x D_y];
E = [E_x E_y];
F = [F_x F_y];
DEF = [D;E;F];
G = [G_x G_y];
H = [H_x H_y];
I = [I_x I_y];
J = [J_x J_y];
GHIJ = [G;H;I;J];
[g1, g2, g3] = ndgrid(1:size(ABC,1), 1:size(DEF,1), 1:size(GHIJ,1));
g123 = [g1(:),g2(:), g3(:)];
all_combs = [ABC(g123(:,1),:),DEF(g123(:,2),:),GHIJ(g123(:,3),:)];
The g* variables are indices. Generate all combinations of indices using ndgrid(), and then use the indices to index into the respective dataset.
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!