how can i find a special row

How can i find a row which including only -1. In that A matrix how can i write a code
A=[-1,-1,0,0,0;0,0,0,-1,0;0,1,-1,0,-1;0,0,1,0,0;1,0,0,1,1]

 Respuesta aceptada

Ravi Jhou
Ravi Jhou el 7 de Jun. de 2013

0 votos

You could try
blnA = logical( A == -1 );
blnOut = find( sum( blnA' ) == 1 );
variable 'blnA' finds the elements of matrix A, which are equal to -1.
The output of blnA is
blnA =
1 1 0 0 0
0 0 0 1 0
0 0 1 0 1
0 0 0 0 0
0 1 0 0 0
if there is just one '-1', the summary for each row of logical matrix blnA will be '1'.
And because the command 'sum' sums the elements of column.
So need to do a transpose.
The output of blnOut
blnOut =
2
A ha, row 2. Got it.

4 comentarios

Light
Light el 7 de Jun. de 2013
Thank you very much!! Please check my related question please... I have to change my whole matrix zero
Light
Light el 7 de Jun. de 2013
Sorry bro!
A=[-1,1,1;0,-1,0;0,0,-1;1,0,0];
In that matrix
blnOut =
1 2 3
I have to find row which include only one -1 or other element in row must be 0 not 1. In that matrix including 1 is found too. Please look at that again please
Ravi Jhou
Ravi Jhou el 7 de Jun. de 2013
You confused me.
Let me describe what you want.
e.g.
A = [-1 1 1 ;
0 -1 0 ;
0 0 -1 ;
1 0 0];
you want to find row 1, 2 ,and 3, which are included at least one '-1'.
or, you want to find row 2 and 3, which are included only one non-zero element '-1'.
You have to describe your question clearly, so I could answer you exactly.
Light
Light el 7 de Jun. de 2013
Thank you for your big help ^_^
i want to find row 2 and 3, which are included only one non-zero element '-1
And i found it THANK YOU

Iniciar sesión para comentar.

Más respuestas (1)

Roger Stafford
Roger Stafford el 6 de Jun. de 2013

0 votos

I am guessing that you mean, how can you find those rows each of which includes at least one -1. Is that what you mean? If so, do this:
f = find(any(A==-1,2));

3 comentarios

Light
Light el 7 de Jun. de 2013
Editada: Light el 7 de Jun. de 2013
Thank you. But i wanna find a row which including only one -1 not 2 or more.
Light
Light el 7 de Jun. de 2013
In a big matrix so many row which including only one -1 can be found. Then i have to choose one of them. How can i code it?
rows = find( sum(A == -1, 2) == 1 );

Iniciar sesión para comentar.

Categorías

Más información sobre Linear Algebra en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 6 de Jun. de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by