Generate all possible combinations from 2 vectors

2 ビュー (過去 30 日間)
gsourop
gsourop 2021 年 5 月 28 日
コメント済み: John D'Errico 2021 年 5 月 28 日
Hi all,
I would like to generate a new matrix that will contain all possible combinations from 2 other matrices. For example,
A = randn(100,3);
B = randn(100,3);
The desired output should be a cell array. such as
C{1} = [A(:,1) B(:,1)];
C{2} = [A(:,1) B(:,1:2)];
C{3} = [A(:,1) B(:,1:3)];
C{4} = [A(:,1:2) B(:,1)];
C{5} = [A(:,1:2) B(:,1:2)];
C{6} = [A(:,1:2) B(:,1:3)];
C{7} = [A(:,1:3) B(:,1)];
C{8} = [A(:,1:3) B(:,1:2)];
C{9} = [A(:,1:3) B(:,1:3)];
Is there a more optimal way to generate this cell array?
Thank you in advance.
  2 件のコメント
gsourop
gsourop 2021 年 5 月 28 日
編集済み: gsourop 2021 年 5 月 28 日
I guess something like this could do the trick, but is this the most efficient way?
A = randn(10,3);
B = randn(10,3);
v2slength = size(A,2)*size(B,2);
for i = 1:3
for j = 1:3
for k = 1:v2slength
Vec2BeStored{k} = [A(:,1:i) B(:,1:j)];
end
end
end
John D'Errico
John D'Errico 2021 年 5 月 28 日
Does it work, doing what you want? Is the time required to do this a problem?
Don't waste your own time looking for a slightly better solution to a non-problem. That is a classic mistake made by people.

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

回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by