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

18 ビュー (過去 30 日間)
Tuhin Choudhury 2020 年 9 月 8 日
コメント済み: Adam Danz 2020 年 9 月 8 日
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 2020 年 9 月 8 日
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 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
Tuhin Choudhury 2020 年 9 月 8 日

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 2020 年 9 月 8 日

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 件のコメント3 件の古いコメントを表示3 件の古いコメントを非表示
Tuhin Choudhury 2020 年 9 月 8 日

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 :) )
Adam Danz 2020 年 9 月 8 日
"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 🙂.

サインインしてコメントする。

### カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

R2019a

### Community Treasure Hunt

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

Start Hunting!

Translated by