Cartesian product of list

6 次查看(过去 30 天)
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.

采纳的回答

Guillaume
Guillaume 2019-1-14
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 个评论
Guillaume
Guillaume 2019-1-14
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')
laurent jalabert
laurent jalabert 2019-1-14
Fantastic !!! Thank you Guillaume !!!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by