finding the indices of the first (by index) negative number in the bottom row of a matrix
Mostrar comentarios más antiguos
I have T = [ 1 2 1 0 0 0 120 ; 1 1 0 1 0 0 70 ; 2 1 0 0 1 0 100 ; -5 -4 0 0 0 1 0 ]
I am interested in finding the indices of the first (by index) negative number in the bottom row of any matrix T. Here, I want the index 4 1, in some form, back.
thanks!!!!
Respuestas (3)
Matt Fig
el 24 de Mzo. de 2011
An alternative,
R = size(T,1);
LOC = [R find(T(R,:)<0,1,'first')]
5 comentarios
Walter Roberson
el 24 de Mzo. de 2011
Matt, what happens when you try that on
T = zeros(7,4);
Matt Fig
el 25 de Mzo. de 2011
In that case, length(LOC)==1.
I assume brittany would have to check the results either way. With your method a call to ISEMPTY will check - either way a check should be made.
Paulo Silva
el 25 de Mzo. de 2011
I think you dont need to use the argument 'first', here's a compact form:
LOC=[size(T,1) find(T(end,:)<0,1)]
Paulo Silva
el 25 de Mzo. de 2011
+1 vote just because I ended up with a similar solution, Walter solution is also very good!
Matt Fig
el 25 de Mzo. de 2011
Good point Paulo! I forgot about that line in the help:
I = FIND(X,K,'first') is the same as I = FIND(X,K)
Walter Roberson
el 24 de Mzo. de 2011
[r,c] = find(T(end,:) < 0,1);
if ~isempty(r); r(:) = size(T,1); end
brittany adam
el 25 de Mzo. de 2011
0 votos
1 comentario
Jan
el 25 de Mzo. de 2011
Does this mean, that you accept one of the answers? Then please click on the corresponding button, such that others can see, that the question is solved.
Categorías
Más información sobre Matrix Indexing en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!