Finding first rightmost non-zero column of a matrix?

How can I tell MATLAB to find the index of the RIGHTmost non-zero column of said matrix?
I found a similar question and answer for the leftmost column and it worked great! However, I am having trouble finding the rightmost column.
I need the leftmost column.
PLS&THANKYOU!

 Respuesta aceptada

dpb
dpb el 15 de Ag. de 2019
Editada: dpb el 15 de Ag. de 2019
Same solution as the other except search from the other end...
find(any(A),1,'last')
I think Azzi's solution there doesn't find the column positionally but will find the column with the largest number of nonzero elements in it. So
[2 1
0 1
0 1]
with his would return 2, not 1 because three rows of second column return T but only one of column 1 will. But both have nonzero elements.
any() does the logical test by column for there being one or more nonzero elements so doesn't matter how many there are as long is one it'll show up. Then the find() locates either first or last depending on which direction you ask for with the optional argument.

5 comentarios

I think you are correct. I tried
find(any(A),1,'last')
but it returned a number outside of my column range. I have a matrix with 231 columns and it returned 683. Do you know another way that I can have matlab return just the number of the last column containing any nonzero value?
Thank you for your help.
dpb
dpb el 15 de Ag. de 2019
Editada: dpb el 16 de Ag. de 2019
"find(any(A),1,'last') ... returned a number outside of my column range."
That's not possible with a 2D matrix...something is inconsistent somewhere.
any(A) by default operates on the columns of a 2D array and returns a row vector of length==size(A,2). (*) The last nonzero location in that vector has to be that number or less.
Here's a couple examples...smaller so can see but there's no difference in logic based on size--
>> A=[zeros(5) randn(5)]; A=A(:,randperm(10));
>> A
A =
0.0662 0 -0.6509 1.2503 0 0 0.0000 0 0 1.1921
0.6524 0 0.2571 0.9298 0 0 -0.0549 0 0 -1.6118
0.3271 0 -0.9444 0.2398 0 0 0.9111 0 0 -0.0245
1.0826 0 -1.3218 -0.6904 0 0 0.5946 0 0 -1.9488
1.0061 0 0.9248 -0.6516 0 0 0.3502 0 0 1.0205
>> find(any(A),1,'last')
ans =
10
>> A=A(:,randperm(10));
>> [A;any(A)]
ans =
-0.6509 1.1921 0 0 0.0000 1.2503 0 0 0.0662 0
0.2571 -1.6118 0 0 -0.0549 0.9298 0 0 0.6524 0
-0.9444 -0.0245 0 0 0.9111 0.2398 0 0 0.3271 0
-1.3218 -1.9488 0 0 0.5946 -0.6904 0 0 1.0826 0
0.9248 1.0205 0 0 0.3502 -0.6516 0 0 1.0061 0
1.0000 1.0000 0 0 1.0000 1.0000 0 0 1.0000 0
>> find(any(A),1,'last')
ans =
9
>>
where the second above also outputs the result of any() as the last line displayed--as noted, it's a logical row vector (converted to double by the concatenation into the array) of the number of columns a A; and is 1 for columns with nonzero elements and zero for those without. Hence, find() on that vector simply cannot return a value outside the range 1:10.
Post your code and the .mat file with the data...
Oh, what does
whos yourArray
return (where "yourArray" is your variable name of this array).
(*) If the 2D array is degenerate and has only one row or column (or, iow, is a vector), then any operates over the non-singleton dimension and will return only a scalar. In that case the result will always be either 1 or empty.
The value can be higher than you expect if you are using an rgb image..
find(any(any(A, 3),1),1,'last')
dpb
dpb el 16 de Ag. de 2019
I just noticed the "image" tag so that's a possibility...something inconsistent with the assumption matrix meant 2D array :)
Walter Roberson's answer worked! But I really appreciate everyones help very much !!! :D I apologize for any confusion on my part, I am very new to MATLAB.

Iniciar sesión para comentar.

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by