"nchoosek" function for a matrix whose all elements are 0 or 1

1 Ansicht (letzte 30 Tage)
Ismaeel
Ismaeel am 29 Dez. 2016
Kommentiert: Ismaeel am 29 Dez. 2016
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 Kommentare
the cyclist
the cyclist am 29 Dez. 2016
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?
Ismaeel
Ismaeel am 29 Dez. 2016
Thanks, yes all possible 1's without considering the 0's. But I want the locations of these possible 1's to be kept.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

James Tursa
James Tursa am 29 Dez. 2016
Bearbeitet: James Tursa am 29 Dez. 2016
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;
  2 Kommentare
Ismaeel
Ismaeel am 29 Dez. 2016
So Cool, thank you so much James.
Ismaeel
Ismaeel am 29 Dez. 2016
Can we modify the code in case if A is a matrix instead of a vector? let's say: A=[0 0 1 0 0 1 0 1 0 0] [0 1 0 0 0 1 0 0 0 0] Thanks once again

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by