Hi, I am having a problem with part of my homework. I have a column with numbers and NaN-s. If I have 5 or more NaN-s in a row I need to know place/position where NaN start (j) and end (i) ( in all column). For example 7 6 5 NaN NaN NaN NaN NaN NaN 9 7
j=4 i=9 Thanks you very much :)

 채택된 답변

Roger Stafford
Roger Stafford 2013년 12월 7일

1 개 추천

You can use the 'find' and 'diff' functions as well as 'isnan' to accomplish that. Let A be your column vector of elements with possible NaNs among them.
m = 5; % The minimum length of consecutive NaNs to record
f = find(diff([false;isnan(A);false]));
B = [f(1:2:end-1),f(2:2:end)-1]; % Get all start and end indices for NaN consecutive sequences
B = B(B(:,2)-B(:,1)+1>=m,:); % Eliminate all with fewer than m NaNs
The first column of array B contains the start indices and the second column the end indices for consecutive sequences of at least five NaNs.

추가 답변 (2개)

Mohammad Monfared
Mohammad Monfared 2013년 12월 7일

0 개 추천

Suppose you have a column or row vector named 'A',
find(isnan(A))

댓글 수: 4

Martina
Martina 2013년 12월 7일
But this gives all NaN-s position. I need positions only when I have 5 or more NaN-s. And only position of first and last NaN because I have vector with 60000 numbers. Thank you
Mohammad Monfared
Mohammad Monfared 2013년 12월 7일
편집: Mohammad Monfared 2013년 12월 7일
If there are just one group of NaNs:
j=find(isnan(A),1,'first')
i=find(isnan(A),1,'last')
Is that your case?
Martina
Martina 2013년 12월 7일
But I have lot of groups :(
Mohammad Monfared
Mohammad Monfared 2013년 12월 7일
It's not a straightforward simple line job, to me. One trick is to use the diff(isnan(A)) to find the boundaries of NaN groups and one more 'diff' to get the length of each group. Then a loop might be used afterward. Think about it (since it is a homework ;) ) a bit. If no success, I'll give the answer.

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

Image Analyst
Image Analyst 2013년 12월 7일
편집: Image Analyst 2013년 12월 7일

0 개 추천

Really easy if you have the Image Processing Toolbox. Do you have that?
measurements = regionprops(isnan(vector), 'PixelIdx');
% just 2 or 3 lines more and you've got it.
By the way, you shoudl have tagged it as homework - I did this for you this time.

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

2013년 12월 7일

답변:

2013년 12월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by