A is 1 by a matrix, B 1 by b, and C 1 by c. D is supposed to be the set of all non-repeating selection of respectively A, B, and C columns. What would be the most economical code of triples extraction? Thanks.

 採用された回答

Dyuman Joshi
Dyuman Joshi 2023 年 12 月 27 日
編集済み: Dyuman Joshi 2023 年 12 月 28 日

0 投票

%Random data
A = 1:3;
B = 1:4;
C = 1:5;
D = {A, B, C}
D = 1×3 cell array
{[1 2 3]} {[1 2 3 4]} {[1 2 3 4 5]}
n = numel(D);
%Preallocate
out = cell(1, n);
%Reverse order to get the lexicographical order when concatenating
[out{end:-1:1}] = ndgrid(D{end:-1:1});
%Concatenate and reshape the data corresponding to number of vectors
out = reshape(cat(n,out{:}),[],n);
disp(out)
1 1 1 1 1 2 1 1 3 1 1 4 1 1 5 1 2 1 1 2 2 1 2 3 1 2 4 1 2 5 1 3 1 1 3 2 1 3 3 1 3 4 1 3 5 1 4 1 1 4 2 1 4 3 1 4 4 1 4 5 2 1 1 2 1 2 2 1 3 2 1 4 2 1 5 2 2 1 2 2 2 2 2 3 2 2 4 2 2 5 2 3 1 2 3 2 2 3 3 2 3 4 2 3 5 2 4 1 2 4 2 2 4 3 2 4 4 2 4 5 3 1 1 3 1 2 3 1 3 3 1 4 3 1 5 3 2 1 3 2 2 3 2 3 3 2 4 3 2 5 3 3 1 3 3 2 3 3 3 3 3 4 3 3 5 3 4 1 3 4 2 3 4 3 3 4 4 3 4 5

4 件のコメント

Dyuman Joshi
Dyuman Joshi 2023 年 12 月 28 日
What is the expected output in this case?
Is the one given by this code not the one you want?
A = [2 3 4];
B = 5;
C = [6 8];
D = {A, B, C}
D = 1×3 cell array
{[2 3 4]} {[5]} {[6 8]}
n = numel(D);
%Preallocate
out = cell(1, n);
%Reverse order to get the lexicographical order when concatenating
[out{end:-1:1}] = ndgrid(D{end:-1:1});
%Concatenate and reshape the data corresponding to number of vectors
out = reshape(cat(n,out{:}),[],n);
disp(out)
2 5 6 2 5 8 3 5 6 3 5 8 4 5 6 4 5 8
Dyuman Joshi
Dyuman Joshi 2023 年 12 月 28 日
Are using the same code as above?
Please share the full error message i.e. all of the red text.
Amir Mahmoudi
Amir Mahmoudi 2023 年 12 月 28 日
Your code is working precisely. I had made a minor mistake. Thanks.
Amir Mahmoudi
Amir Mahmoudi 2023 年 12 月 28 日
I deleted my previous comments.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by