Filter löschen
Filter löschen

All possible Permutations for vector

1 Ansicht (letzte 30 Tage)
Marco Gaetani d'Aragona
Marco Gaetani d'Aragona am 19 Mär. 2021
Hi,
I want to create a permutation starting from a vector. For example:
V=[3 3 5]
I want every possible permutation considering values lower or equal to that of the each column, so that the first column can contain number from 1 to 3, the second from 1 to 3 and the third from 1 to 5, without repetitions. In this case:
1 1 1
2 1 1
3 1 1
1 2 1
2 2 1
3 2 1
1 3 1
2 3 1
3 3 1
1 1 2
2 1 2
3 1 2
....
3 3 5
Any suggestion on how to do that?
Thank you

Akzeptierte Antwort

Matt J
Matt J am 19 Mär. 2021
V=[3,3,5];
n=numel(V);
Vc=cellfun(@(x) 1:x, num2cell(V), 'uni',0);
[C{1:n}]=ndgrid(Vc{:});
out=reshape( cat(n+1,C{:}) ,[],n)
out = 45×3
1 1 1 2 1 1 3 1 1 1 2 1 2 2 1 3 2 1 1 3 1 2 3 1 3 3 1 1 1 2

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by