Need Help in vectors combinations

5 ビュー (過去 30 日間)
Mahmoud Ahmed
Mahmoud Ahmed 2017 年 6 月 6 日
コメント済み: Walter Roberson 2017 年 6 月 7 日
I have 2 variables (A,B) .. The first variable (A) consists of 3 vectors of size (1,9):
A(:,:,1) = [1 2 3 4 5 6 7 8 9]
A(:,:,2) = [1 4 7 2 5 8 3 6 9]
A(:,:,3) = [3 6 9 2 5 8 1 4 7]
The second variable (B) consists of 2 vectors of size (1,9):
B(:,:,1) = [11 12 13 14 15 16 17 18 19]
B(:,:,2) = [11 14 17 12 15 18 13 16 19]
How can I obtain all combinations of matrix (C) with size of (2,9) that conisits of A,B vectors for example: I want to obtain:
C(:,:,1) = [A(:,:,1) ; B(:,:,1)]
C(:,:,2) = [A(:,:,1) ; B(:,:,2)]
C(:,:,3) = [A(:,:,2) ; B(:,:,1)]
C(:,:,4) = [A(:,:,2) ; B(:,:,2)]
C(:,:,5) = [A(:,:,3) ; B(:,:,1)]
C(:,:,6) = [A(:,:,3) ; B(:,:,2)]

採用された回答

Stephen23
Stephen23 2017 年 6 月 6 日
編集済み: Stephen23 2017 年 6 月 6 日
Use meshgrid and vertcat:
>> [Xa,Xb] = meshgrid(1:size(A,3),1:size(B,3));
>> C = vertcat(A(:,:,Xa(:)),B(:,:,Xb(:)))
C(:,:,1) =
1 2 3 4 5 6 7 8 9
11 12 13 14 15 16 17 18 19
C(:,:,2) =
1 2 3 4 5 6 7 8 9
11 14 17 12 15 18 13 16 19
C(:,:,3) =
1 4 7 2 5 8 3 6 9
11 12 13 14 15 16 17 18 19
C(:,:,4) =
1 4 7 2 5 8 3 6 9
11 14 17 12 15 18 13 16 19
C(:,:,5) =
3 6 9 2 5 8 1 4 7
11 12 13 14 15 16 17 18 19
C(:,:,6) =
3 6 9 2 5 8 1 4 7
11 14 17 12 15 18 13 16 19
  2 件のコメント
Mahmoud Ahmed
Mahmoud Ahmed 2017 年 6 月 7 日
編集済み: Mahmoud Ahmed 2017 年 6 月 7 日
Actually, I have 10 variables .. One of them consists of 83 vectors .. I replace meshgrid with ndgrid .. but when I use it, MATLAB gives me error and said:
Error using ones
Maximum variable size allowed by the program is exceeded.
Error in ndgrid (line 69)
x = reshape(x(:,ones(1,prod(s))),[length(x) s]); % Expand x
What can I do ?
Walter Roberson
Walter Roberson 2017 年 6 月 7 日
What you can do is change how you are solving your problem. You are trying to produce about 2^49 different combinations, which cannot possibly fit in memory as no released x64 architecture CPU supports more than 48 address lines.

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by