Finding matrix column index based on certain conditions

3 vues (au cours des 30 derniers jours)
Lucas Carvalho
Lucas Carvalho le 30 Juil 2017
Commenté : Lucas Carvalho le 31 Juil 2017
Hello guys,
I want to extract the column indexes of a matrix which contains necessarily one element equals to 1 and all the other equal to zero. Matrix example:
A =
1 1 1 0 0 0 0 1 0 0 0 0 0
0 -1 -1 -1 0 0 0 -1 1 0 0 0 0
0 0 0 1 0 -1 0 0 0 1 0 0 0
0 0 1 0 1 0 -1 0 0 0 1 0 0
0 0 0 0 0 1 1 0 0 0 0 1 0
0 1 0 0 -1 0 0 0 0 0 0 0 1
The columns of this matrix which obey these conditions are: 1,9,10,11,12 and 13.
How can I implement these rules into my code??
Thank you very much indeed!

Réponse acceptée

James Tursa
James Tursa le 31 Juil 2017
result = find(sum(A==1)==1 & sum(A==0)==(size(A,1)-1));

Plus de réponses (1)

Walter Roberson
Walter Roberson le 31 Juil 2017
find( sum(A == 1) == 1 & sum(A == 0) == size(A,1)-1 )

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by