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

Stephen23
Stephen23 2019년 2월 22일
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.

댓글을 달려면 로그인하십시오.

 채택된 답변

madhan ravi
madhan ravi 2019년 2월 21일
편집: madhan ravi 2019년 2월 21일

1 개 추천

댓글 수: 9

A = [NaN NaN NaN 1 2 3 4 NaN 3 44 NaN] ; % example data
index=find(~isnan(A)); % will give you the length of numbers inbetween nan , if you remove ~ it will give the length of nans inbetween numbers
idx=find(diff(index)~=1);
R=[idx(1) diff(idx) numel(index)-idx(end)]
Lieke Numan
Lieke Numan 2019년 2월 21일
Thanks a lot!
madhan ravi
madhan ravi 2019년 2월 21일
편집: madhan ravi 2019년 2월 21일
Jos did you read the comment first? Just remove ~ infront of isnan() please examine before flagging.
Jos (10584)
Jos (10584) 2019년 2월 21일
My apologies, I will remove the flag and comment.
Stephen23
Stephen23 2019년 2월 22일
편집: Stephen23 2019년 2월 22일
Note that this answer fails in many simple cases, e.g.:
All 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.
No NaN
>> 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.
Its use of indexing means that any special cases will need to be handled separately.
See my answer for simpler code that works for all of these cases.
madhan ravi
madhan ravi 2019년 2월 22일
Just an if condition should suffice that need.
Stephen23
Stephen23 2019년 2월 22일
편집: Stephen23 2019년 2월 22일
"Just an if condition should suffice that need. "
Please show exactly how one simple IF statement would fix those cases.
madhan ravi
madhan ravi 2019년 2월 22일
Turns out not possible :D.
madhan ravi
madhan ravi 2019년 2월 22일
@Lieke there are so many limitations to this answer , you have accepted the wrong answer ;-)

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Stephen23
Stephen23 2019년 2월 21일
편집: Stephen23 2019년 2월 22일

1 개 추천

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

madhan ravi
madhan ravi 2019년 2월 21일
편집: madhan ravi 2019년 2월 21일
Even the accepted answer works as mentioned exactly in the comment.
I accept it's simpler than the accepted answer but I don't think it's faster:
>> A = [NaN NaN NaN 1 2 3 4 NaN 3 44 NaN];
tic
for k=1:1e5
D = diff([0,isnan(A),0]);
find(D<0)-find(D>0);
end
toc
tic
for k=1:1e5
index=find(isnan(A));
idx=find(diff(index)~=1);
R=[idx(1) diff(idx) numel(index)-idx(end)];
end
toc
Elapsed time is 1.533159 seconds.
Elapsed time is 0.308740 seconds.
>>
A diff-find-diff solution :-D
D = diff(find(diff([0,isnan(A),0])))
R = D(1:2:end)
Stephen23
Stephen23 2019년 2월 22일
"Even the accepted answer works as mentioned exactly in the comment."
Except when it doesn't work:
madhan ravi
madhan ravi 2019년 2월 22일
Yes but in this thread :D.
Stephen23
Stephen23 2019년 2월 22일
"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
Jan 2019년 2월 22일
편집: Jan 2019년 2월 22일

1 개 추천

And again: With FEX: RunLength :
[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;
the method posted by Jos (link) is about 10% faster:
D = diff(find(diff([false, isnan(A), false])));
R = D(1:2:end);
KSSV
KSSV 2019년 2월 21일

0 개 추천

Read about isnan and nnz

댓글 수: 4

madhan ravi
madhan ravi 2019년 2월 21일
nnz will give you the total number of elements not the length of each array of nans
KSSV
KSSV 2019년 2월 21일
A = [NaN NaN NaN 1 2 3 4 NaN] ;
nnz(isnan(A))
There are four NaN's.
madhan ravi
madhan ravi 2019년 2월 21일
편집: madhan ravi 2019년 2월 21일
OP wants it like 3 nans and 1 nan not the total number of nans or the length of each numbers inbetween nans.
KSSV
KSSV 2019년 2월 21일
he can use diff anddo that..

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Large Files and Big Data에 대해 자세히 알아보기

태그

질문:

2019년 2월 21일

댓글:

2019년 2월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by