"nchoosek" function for a matrix whose all elements are 0 or 1
2 ビュー (過去 30 日間)
古いコメントを表示
I have a matrix A whose all elements are 0 or 1. For example, the first row of A is:
[ 0 0 1 0 0 1 0 1 0 0]
I want to use nchoosek function to find the combinations of (total number of 1's in a row) -1. Here, n= 3, k=n-1=2 (always k=n-1). How can I apply this function to all the rows of the matrix or at least, for the above vector.
The output should be as:
[ 0 0 1 0 0 1 0 0 0 0]
[ 0 0 1 0 0 0 0 1 0 0]
[ 0 0 0 0 0 1 0 1 0 0]
nchoosek (3,2)=3.
Thanks
2 件のコメント
the cyclist
2016 年 12 月 29 日
For the example
A = [0 0 1 0 0 1 0 1 0 0]
you want all possible rows that have two 1's and how many 0's?
採用された回答
James Tursa
2016 年 12 月 29 日
編集済み: James Tursa
2016 年 12 月 29 日
E.g., using a loop:
f = find(A);
n = numel(f);
result = repmat(A,n,1);
for k=1:n
result(k,f(k)) = 0;
end
Or if the order matters:
result(k,f(n-k+1)) = 0;
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!