简体   繁体   中英

MATLAB - How to find the last row in a column with the value greater than zero (or before sequence of NaNs)?

I have columns of values that decrease to ~1, after which all remaining values are NaNs. For example:

10    10
8     9
6     8
5     7
3     5
1     2
NaN   1
NaN   NaN

What I need is the row number of each column immediately before the sequence of NaNs, or the last positive integer in the sequence. Row 6 and row 7 in the example above.

While a loop can probably be used to find the desired row of each column, I'm unable to identify the correct command to find this row. I attempted using find 'last' without success. Please advise the best way to achieve this. Thanks.

if your matrix is a , you can use

 sum(isfinite(a))

ans =
     6     7

this means that for the first col it is the 6th row, and for second col it is the 7th row that have the last non-NaN values.

this is alternatively the same: sum(~isnan(a)) ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM