Generating a select set of permutations

1 回表示 (過去 30 日間)
Mason Dyess
Mason Dyess 2019 年 8 月 15 日
コメント済み: Mason Dyess 2019 年 8 月 15 日
So for example say I have a vector of length 17. I can fill each element with a -1, 0 or a 1, and I want MATLAB to output the permutations for this situation. However, I realize that trying to output all the permutations would be way too large and unfeasible (3^17 for my example), and I'm only really interested in the permutations that have a low number of non-zero elements (Ex: Only 4 of the 17 elements in the vector are a 1 or -1 and the rest are zeros). Is there a way to generate just this select set of permutations?

採用された回答

Bruno Luong
Bruno Luong 2019 年 8 月 15 日
m = 17;
nnz = 4;
p=(nchoosek(1:m,nnz));
q=2*(dec2bin(0:2^nnz-1)-'0')-1;
C=repelem(p,size(q,1),1);
R=repmat((1:size(C,1))',1,size(C,2));
V=repmat(q,size(p,1),1);
A = accumarray([R(:) C(:)],V(:))
  1 件のコメント
Mason Dyess
Mason Dyess 2019 年 8 月 15 日
Thank you so much Bruno Luong. I honestly was skeptical that this scenario was even solvable, but your solution works exactly like I envisioned. Thanks again!

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by