Replacing the last value after consecutive Nan

Hello everyone,
I would like to know how to replace a stand alone value that's greater than zero and follows a row of Nans. I would eventually replace that value with a 0.
For example, A = [ 0 0 0 Nan Nan Nan 1.2 0 0 Nan Nan 2.3 1 2 5], as you can see in A the value that I would want to replace by 0 is 1.2, I would like it to A = [ 0 0 0 Nan Nan Nan 0 0 0 Nan Nan 2.3 1 2 5]. I'm not sure how to approach this. Do I try to find the indices of the non nan values? and if I do that what would be next.
Help would be much appreciated.
Thank you in advance!

댓글 수: 3

Bhaskar R
Bhaskar R 2019년 11월 13일
What about the value 2.3 after NaN ? Should it need to be 0?
M
M 2019년 11월 13일
Do you want to replace the value after 3 consecutive NaN or after any NaN values ?
In any case, you can start by looking at the isnan function.
John Doe
John Doe 2019년 11월 13일
No, since it isn't a stand alone value, I want to keep it as it is along with the values that follow it. By stand alone I mean the only value that's greater than zero, following a Nan

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

 채택된 답변

Stijn Haenen
Stijn Haenen 2019년 11월 13일

0 개 추천

Try this:
for i=2:numel(A)
if isnan(A(i-1))&&A(i)>0
A(i)=0;
end
end

추가 답변 (0개)

카테고리

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

질문:

2019년 11월 13일

댓글:

2019년 11월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by