How to change a Data Series contained with Repeated NaNs to become other sequence of NaNs?

조회 수: 2 (최근 30일)
Hi, community
I want to ask something that is still difficult for me to generate the code in Matlab. The data is simple, that is :
A = [NaN, NaN, 0, 0, 0, NaN, NaN, 0, 0, 0, 0, 0, NaN, NaN, 0, NaN, NaN];
And i just want to re-create the data A, with double NaN unique data, to become :
B = [NaN, 0, 0, 0, 0, NaN, 0, 0, 0, 0, 0, 0, NaN, 0, NaN, 0];
And so on for every double NaN of Data A were changed to become Data B with a single NaN unique data?
Its so difficult for me to create such a code for data A to become data B.... Anyone in Community culd help me in solving my problem here? Thank you so much, im so grateful if someone can help me out.... /.\ /.\ /.\
  댓글 수: 2
Stephen23
Stephen23 2021년 12월 31일
What should happen if there are three or more sequential NaNs ?
Tyann Hardyn
Tyann Hardyn 2021년 12월 31일
편집: Tyann Hardyn 2021년 12월 31일
If three or more sequent of NaN then it would be :
A = [NaN, NaN, NaN, 0, 0, NaN, NaN, 0, 0, 0, 0, 0, NaN, NaN, NaN, NaN, NaN];
and should become :
B = [NaN, NaN, 0, 0, 0, NaN, 0, 0, 0, 0, 0, 0, NaN, NaN, NaN, NaN, 0];
I just want to remove the last sequance of NaN, Sir... So the last sequence of NaNs should be converted to zero (0) without changing the length of A and B....

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

채택된 답변

Stephen23
Stephen23 2021년 12월 31일
A = [NaN, NaN, NaN, 0, 0, NaN, NaN, 0, 0, 0, 0, 0, NaN, NaN, NaN, NaN, NaN]
A = 1×17
NaN NaN NaN 0 0 NaN NaN 0 0 0 0 0 NaN NaN NaN NaN NaN
X = diff([isnan(A),false])<0;
A(X) = 0
A = 1×17
NaN NaN 0 0 0 NaN 0 0 0 0 0 0 NaN NaN NaN NaN 0
  댓글 수: 3
Image Analyst
Image Analyst 2022년 1월 1일
This is the only one that worked. We finally clarified what you want -- that only the last nan in a sequence of nans should be set to zero, regardless of how many nans there are in the sequence (as long as it's 2 or longer). The answers by Chunru and Walter don't work in all cases.
So to give @Stephen the credit for the correct Answer, could you change the Accepted answer to Stephen's? 🙂
Tyann Hardyn
Tyann Hardyn 2022년 1월 27일
@Image Analyst, thx for your Great Help. Im sorry, cuz i wrongly put the accepted answer.... You re helping me so much

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

추가 답변 (2개)

Chunru
Chunru 2021년 12월 31일
편집: Chunru 2021년 12월 31일
A = [NaN, NaN, 0, 0, 0, NaN, NaN, 0, 0, 0, 0, 0, NaN, NaN, 0, NaN, NaN];
idx = isnan(A);
% any repeated nan will be replaced with 0
A(idx == 1 & diff([0 idx]) == 0) = 0
A = 1×17
NaN 0 0 0 0 NaN 0 0 0 0 0 0 NaN 0 0 NaN 0
% replace last nan in sequence with 0
A = [NaN, NaN, NaN, 0, 0, NaN, NaN, 0, 0, 0, 0, 0, NaN, NaN, NaN, NaN, NaN];
idx = isnan(A);
A(diff([idx 0])==-1) = 0
A = 1×17
NaN NaN 0 0 0 NaN 0 0 0 0 0 0 NaN NaN NaN NaN 0

Walter Roberson
Walter Roberson 2021년 12월 31일
3 tests to be sure the boundary tests are handled correctly -- ending in double nan, ending in single nan, ending in no nan.
Should probably have similar tests about starting with variable number of nan.
%actual work
DN = @(V) V(~isnan(V) | [~isnan(V(2:end)), true]);
%rest is testing
A = [NaN, NaN, 3, 5, 7, NaN, NaN, 2, 4, 6, 8, 10, NaN, NaN, -3, NaN, NaN]
A = 1×17
NaN NaN 3 5 7 NaN NaN 2 4 6 8 10 NaN NaN -3 NaN NaN
B = DN(A)
B = 1×13
NaN 3 5 7 NaN 2 4 6 8 10 NaN -3 NaN
A2 = A(1:end-1)
A2 = 1×16
NaN NaN 3 5 7 NaN NaN 2 4 6 8 10 NaN NaN -3 NaN
B2 = DN(A2)
B2 = 1×13
NaN 3 5 7 NaN 2 4 6 8 10 NaN -3 NaN
A3 = A(1:end-2)
A3 = 1×15
NaN NaN 3 5 7 NaN NaN 2 4 6 8 10 NaN NaN -3
B3 = DN(A3)
B3 = 1×12
NaN 3 5 7 NaN 2 4 6 8 10 NaN -3
  댓글 수: 3
Image Analyst
Image Analyst 2021년 12월 31일
@Tyann Hardyn you are not converting "the last sequence of NaNs should be converted to zero (0)" What your example actually showed was converting the last NaN of the last sequence of NaNs into a zero. That's an entirely different thing. So which way to you want it.
  1. the last sequence of NaNs (all of them) should be converted to zero (0)
  2. the last zero of the last sequence of NaNs (one NaN value -- the final one -- only) should be converted to zero (0)
Case 1 or case 2? And does that still apply if the last NaN sequence doesn't go right up to the very end of the vector?
Tyann Hardyn
Tyann Hardyn 2022년 1월 1일
In case 2, Sir. Because i just want to convert the last sequence of NaNs to become 0. Im sorry for my bad explanations....

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

Community Treasure Hunt

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

Start Hunting!

Translated by