Cartesian product of list
10 views (last 30 days)
Show older comments
laurent jalabert
on 14 Jan 2019
Commented: laurent jalabert
on 14 Jan 2019
Hello,
I have 4 vectors A, B, C, D with
A=1:6; B=1:5; C=1:10; D=1:4; % in real case, the max values i.e 6, 5, 10 and 4, are not always equal to those values but always integers
ADBC = allcomb(A,D,B,C); % each row corresponds to an experimental condition involving one of each parameter A, B, C, D
Now, I want to reorganize ADBC elements (row) in order to build a new matrix ABCD = allcomb(A,B,C,D);
As an example, I have ADBC = [2 2 3 1], which means A=2, B=3, C=1 and D=2;
The question is what is the row of ABCD corresponding to the same condition (A=2, B=3, C=1, D=2) if it was organized as ABCD, i.e [2 3 1 2] ?
Here is the answer :
A=1:6; B=1:5; C=1:10; D=1:4; % in real case, the max values i.e 6, 5, 10 and 4, are not always equal to those values but always integers
ABCD = allcomb(A,B,C,D); % https://fr.mathworks.com/matlabcentral/fileexchange/10064-allcomb-varargin
ADBC = allcomb(A,D,B,C); % each row corresponds to an experimental condition involving one of each parameter A, B, C, D
i = 0; crush = 1;
while crush ~= 4 % because I seek for 4 values [i j k m]
i=i+1;
check = find(ADBC(i,:) == [2 2 3 1]); % in order "ADBC"
crush = length(check);
end
ADBC(i,:)
i % 271
i = 0; crush2 = 1;
while crush2 ~= 4 % because I seek for 4 values [i j k m]
i=i+1;
check2 = find(ABCD(i,:) == [2 3 1 2]); % after sorting ex1 elements in order"ABCD"
crush2 = length(check2);
end
ABCD(i,:)
i % 282
So I would like to rebuilt the transfer function between the known ADBC and the rebuilt ABCD.
So, I guess I should write a loop (from 1 to 6*5*10*4=1200) to scan each ADBC element (ex1), get the old position (271 in the example), then sort the element with respect to "ABCD" order, then get the new position in case of ABCD vector (282 in the example).
So ABCD(282) = ADBC(271).
Can anyone simplify this approach ?
How to generalize to any ACBD, ADBC, ABDC, ... (24 cases) to rebuild ABCD in any case ? ABCD is my goal.
0 Comments
Accepted Answer
Guillaume
on 14 Jan 2019
This seems like a very odd thing to want to do, but it can be easily achieved with ismember:
[~, whereinADBC] = ismember(ABCD, ADBC, 'rows');
3 Comments
Guillaume
on 14 Jan 2019
I didn't think this through properly. The columns need to be reordered before calling ismember:
[~, whereinADBC] = ismember(ABCD(:, [1 4 2 3]), ADBC, 'rows')
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!