フィルターのクリア

determine combination of elements in matrix

3 ビュー (過去 30 日間)
JL
JL 2019 年 8 月 22 日
編集済み: Adam Danz 2019 年 8 月 22 日
I have a matrix, X
x = [1 1 1 1]
and I know if I introduced one zero to x, possible combination 4^1=4, which gives me, y
y = [0 1 1 1;
1 0 1 1;
1 1 0 1;
1 1 1 0;];
if I introduced two zeros to x, what are possible combinations which gives me, z. Is this correct below? Is there a code to determine the combinations?
z = [0 0 1 1
0 1 0 1
0 1 1 0
1 0 0 1
1 0 1 0
1 1 0 0]

採用された回答

Adam Danz
Adam Danz 2019 年 8 月 22 日
編集済み: Adam Danz 2019 年 8 月 22 日
You can use combnk() to create the permuted column indices for each row of your matrix and then replace those values by 0.
% Generate some data
z = ones(6,4); % your original matrix
nzeros = 2; % number of zeros to add to each row
Solution:
permIdx = combnk(1:size(z,2), nzeros); %permutation index
linIdx = sub2ind(size(z),repmat(1:size(permIdx),nzeros,1).',sortrows(permIdx));
z(linIdx) = 0;
  2 件のコメント
JL
JL 2019 年 8 月 22 日
Thank you. I think this works better
Adam Danz
Adam Danz 2019 年 8 月 22 日
編集済み: Adam Danz 2019 年 8 月 22 日
Glad I could help. If you need to know the number of combinations you can use
numCombo = nchoosek(4,2); % for 4 columns, 2 zeros.
% = 6, so you would need 6 rows.

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

その他の回答 (1 件)

Bruno Luong
Bruno Luong 2019 年 8 月 22 日
編集済み: Bruno Luong 2019 年 8 月 22 日
lgt = 4; % number of elements
nz = 2; % number of 0s
j = nchoosek(1:lgt,lgt-nz);
i = repmat((1:size(j,1))',[1 size(j,2)]);
z = accumarray([i(:),j(:)],1)
  1 件のコメント
JL
JL 2019 年 8 月 22 日
Thank you very much!

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

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by