フィルターのクリア

all possible combinations of vectors of unknown quantity and length

3 ビュー (過去 30 日間)
gabriele fadanelli
gabriele fadanelli 2020 年 8 月 2 日
let's suppose I want to find all combinations of vectors , so I have as for example in input a matrix
A=[2 5 8 9];
which gives me the length of the vectors (from 1 to specific value, 1D vectors) I should compute combinations, i.e. for this specific case a good choice would be:
allComb=combvec(1:2,1:5,1:8,1:9)'
in order to have the combination values written in rows.
The problem is: in my code the INPUT MATRIX A can change values and dimension (2nd) and I don't know how to write a code, because even if I organize in cell the vectors and perform a FOR loop, then I don't know how to call back my vectors in combvec function.
thank you whoever could help me facing this.

採用された回答

Bruno Luong
Bruno Luong 2020 年 8 月 2 日
Generate vectors of consecutive integers to be combined
A = [2 5 8 9];
c = arrayfun(@(n) 1:n, A, 'unif', 0)
Generate combinations
c = flip(c);
[c{:}] = ndgrid(c{:});
c = cellfun(@(x) x(:), flip(c), 'unif', 0);
c = cat(2, c{:});
Display
disp(c)
  3 件のコメント
Bruno Luong
Bruno Luong 2020 年 8 月 2 日
>> combvec
'combvec' requires Deep Learning Toolbox.
As you see I don't have the tbx. My code works with MATLAB alone.
gabriele fadanelli
gabriele fadanelli 2020 年 8 月 2 日
Ok, thank you. This is a nice answer.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by