find length NaN segments
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Lieke Numan
le 21 Fév 2019
Commenté : madhan ravi
le 22 Fév 2019
I have large vectors, containing quite a lot of NaN samples. I want to know the length of each array of NaNs within this vector, even when this equals 1. So I want to have the lenght of all NaN segments.
Thanks in advance!
1 commentaire
Stephen23
le 22 Fév 2019
Note to future readers: the accepted answer fails for many simple cases:
Only NaN
>> A = [NaN,NaN,NaN,NaN];
>> index=find(isnan(A));
>> idx=find(diff(index)~=1);
>> R=[idx(1) diff(idx) numel(index)-idx(end)]
Attempted to access idx(1); index out of bounds because numel(idx)=0.
Only Numbers
>> A = [1,2,3,4];
>> index=find(isnan(A));
>> idx=find(diff(index)~=1);
>> R=[idx(1) diff(idx) numel(index)-idx(end)]
Attempted to access idx(1); index out of bounds because numel(idx)=0.
Empty Vector
>> A = [];
>> index=find(isnan(A));
>> idx=find(diff(index)~=1);
>> R=[idx(1) diff(idx) numel(index)-idx(end)]
Attempted to access idx(1); index out of bounds because numel(idx)=0.
Leading Numbers
>> A = [1,NaN,NaN,NaN];
>> index=find(isnan(A));
>> idx=find(diff(index)~=1);
>> R=[idx(1) diff(idx) numel(index)-idx(end)]
Attempted to access idx(1); index out of bounds because numel(idx)=0.
Trailing Numbers
>> A = [NaN,NaN,NaN,1];
>> index=find(isnan(A));
>> idx=find(diff(index)~=1);
>> R=[idx(1) diff(idx) numel(index)-idx(end)]
Attempted to access idx(1); index out of bounds because numel(idx)=0.
Réponse acceptée
madhan ravi
le 21 Fév 2019
Modifié(e) : madhan ravi
le 21 Fév 2019
https://www.mathworks.com/matlabcentral/answers/444595-count-the-occurence-of-a-number-in-between-other-numbers - replace the first line with isnan().
9 commentaires
madhan ravi
le 22 Fév 2019
@Lieke there are so many limitations to this answer , you have accepted the wrong answer ;-)
Plus de réponses (3)
Stephen23
le 21 Fév 2019
Modifié(e) : Stephen23
le 22 Fév 2019
This is simpler and actually works for all horizontal vectors (unlike the accepted answer):
>> A = [NaN NaN NaN 1 2 3 4 NaN 3 44 NaN];
>> D = diff([false,isnan(A),false]);
>> find(D<0)-find(D>0)
ans =
3 1 1
For a slightly faster version you can call find once:
>> F = find(diff([false,isnan(A),false]));
>> F(2:2:end)-F(1:2:end)
ans =
3 1 1
EDIT: uses Jan's logical vector suggestion.
8 commentaires
Stephen23
le 22 Fév 2019
"Yes but in this thread "
The main difference is swapping == for isnan (which everyone used). I doubt that it makes much difference, but you are welcome to do some tests and post the results here.
Jan
le 22 Fév 2019
Modifié(e) : Jan
le 22 Fév 2019
[B, N] = RunLength(A);
Result = N(isnan(B));
Or:
y = [false, isnan(A), false];
Result = strfind(y, [true, false]) - strfind(y, [false,true])
For this test vector:
A = ones(1, 1e5);
A(randperm(1e5, 5e4)) = NaN;
D = diff(find(diff([false, isnan(A), false])));
R = D(1:2:end);
0 commentaires
KSSV
le 21 Fév 2019
Read about isnan and nnz
4 commentaires
madhan ravi
le 21 Fév 2019
Modifié(e) : madhan ravi
le 21 Fév 2019
OP wants it like 3 nans and 1 nan not the total number of nans or the length of each numbers inbetween nans.
Voir également
Catégories
En savoir plus sur Matrices and Arrays dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!