Find the index of the last numeric element of a column including NaNs

2 visualizzazioni (ultimi 30 giorni)
Cary il 27 Giu 2015
Commentato: Matt J il 27 Giu 2015
I have a column with many rows that has data in the middle surrounded by NaNs. For example, below is an abbreviated version of what I'm talking about. Let's call the column 'x':
NaN NaN NaN 235 6 24 NaN NaN
I am trying to find the index of 24 (the last numeric element in the column). So far I am able to do this:
find(~isnan(x))
The snippet above returns the index of all three numbers. I just want the last one (24). I have tried
find(~isnan(x(end)))
but this gives me an empty array (in the Workspace, the Value is []).
Can anyone shed some light on this. I appreciate everyone's help.
UPDATE: I have found the solution.
find(~isnan(x),1,'last')
Thanks for everyone's help.
0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Risposta accettata

Matt J il 27 Giu 2015
Modificato: Matt J il 27 Giu 2015
find(~isnan(x),1,'last')
1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Matt J il 27 Giu 2015
To do this column-wise when x is a matrix, a slightly different approach is needed,
[~,I]=max(flipud(~isnan(x)));
result=size(x,1)+1-I;

Accedi per commentare.

Più risposte (1)

Azzi Abdelmalek il 27 Giu 2015
find(~isnan(fliplr(x)),1)
0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Categorie

Scopri di più su Matrix Indexing in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by