# Find different possible combinations of the rows of two matrices.

조회 수: 3(최근 30일)
Tuhin Choudhury 8 Sep 2020
댓글: Adam Danz 8 Sep 2020
Consider two matrices:
a = [1 2 3; 4 5 6; 7 8 10]
a =
1 2 3
4 5 6
7 8 10
and b=a + 10
b =
11 12 13
14 15 16
17 18 20
first, I am trying to obtain all possible combination of rows (1st row of 'a' and all rows of 'b', second row of 'a' and all rows of 'b' and so on) such that the output is
c =
1 2 3 11 12 13
4 5 6 11 12 13
7 8 10 11 12 13
1 2 3 14 15 16
4 5 6 14 15 16
7 8 10 14 15 16
1 2 3 17 18 20
4 5 6 17 18 20
7 8 10 17 18 20
secondly for each row, I am storing the first 3 and the last 3 columns in a single index of a 3 index vector. So in this example the vector of combosets is 2x3x9 where element is
combosets(:,:,1) =
1 2 3
11 12 13
combosets(:,:,2) =
4 5 6
11 12 13
and so on. The code I am using is below. Is there any better and faster way to do this and prefarably step 1 and 2 combined?
clc
clearvars;
a = [1 2 3; 4 5 6; 7 8 10]; % 3 by 3 matrix
b=a + 10; % 3 by 3 matrix
r=size(a,1);
combosets=zeros(r-1,r,r*r);
d={NaN};
% Step 1: storing different combination of rows in cell
for k=1:1:r
c=vertcat(horzcat(a(:,:),repmat(b(k,:),r,1)),horzcat(a(:,:),repmat(b(k,:),r,1)),horzcat(a(:,:),repmat(b(k,:),r,1)));
d{k}=(unique(c,'rows'));
end
ddash=cell2mat(d'); % making it a matrix again
% Step 2: storing in a multidimensional array
for j=1:1:r*r
rowset1=ddash(j,1:3);
rowset2=ddash(j,4:6);
combosets(:,:,j)=[rowset1;rowset2];
end

댓글을 달려면 로그인하십시오.

### 채택된 답변

Johannes Fischer 8 Sep 2020
a = [1 2 3; 4 5 6; 7 8 10]; % 3 by 3 matrix
b=a + 10; % 3 by 3 matrix
r=size(a, 1);
% create repetitions of values
a = repmat(a, [r, 1]);
b = repelem(b, r, 1);
% create combination matrix
c = [a b];
% reshape into combosets
combosets = reshape(c', 3, 2, 9);
% permute to get dimensions right
combosets = permute(combosets, [2, 1, 3]);

#### 댓글 수: 1

Tuhin Choudhury 8 Sep 2020
Thank you! works perfectly fine. Infact your method works fastest and so I accepted this answer . although all of them work perfectly fine

댓글을 달려면 로그인하십시오.

### 추가 답변(1개)

편집: Adam Danz 8 Sep 2020
Fast & clean 2-liner:
a = [1 2 3; 4 5 6; 7 8 10];
b = a + 10;
c = [repmat(a,size(b,1),1), repelem(b, size(a,1),1)];
combosets = permute(reshape(c.',3,2,9),[2,1,3]);

#### 댓글 수: 5

표시 이전 댓글 수: 2
FYI, this answer and Johannes' are virtually identical. The biggest difference is that Johannes' answer overwrites the a and b variables. Since my version skips that step, it is faster than Johannes'.
For comparison, 10,000 reps of each answer took 0.102 sec for Johannes' and 0.043 for mine.
Speed isn't everything, though, and Johannes' answer is, perhaps, more readable than mine and that's also important.
Tuhin Choudhury 8 Sep 2020
I completely agree both are identical and I wish I could accept more than 1 answer. True that the readability helps as well. However for the speed, my problem lies with the vector size instead of reps, so initially i tested with 'a' as rand(1050x3) and the results were like:
>> Tuhin
Elapsed time is 2.835849 seconds.
>> Stephen
Elapsed time is 0.124517 seconds.
>> Johannes
Elapsed time is 0.085730 seconds.
Elapsed time is 0.094663 seconds.
Then I realized that in this case the speed might depend on the values generated by rand. So I tested with a fixed matrix 'a' (size (7,3)).The resutls were:
>> Tuhin
Elapsed time is 0.031925 seconds.
>> Stephen
Elapsed time is 0.007881 seconds.
>> Johannes
Elapsed time is 0.002299 seconds.
Elapsed time is 0.002421 seconds.
Like I said, both of you have very identical speed (way faster than mine :) )
"I wish I could accept more than 1 answer"
You accepted the right one if it works for your data and it make sense to you. The time difference is so small that readability and understandability are important factors. Plus, I like when newer contributors get recognition for their contributions and encouragement to continue doing so 🙂.

댓글을 달려면 로그인하십시오.

R2019a

### Community Treasure Hunt

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

Start Hunting!