Cartesian product of list

조회 수: 7 (최근 30일)
laurent jalabert
laurent jalabert 2019년 1월 14일
댓글: laurent jalabert 2019년 1월 14일
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개)

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by