Hi there, I am new in matlab and try to solve this problem for couple of hours, I need to replace all NaNs with the value in previous column and add 100:
so now I have> [14] [NaN] [9] [NaN] [13] [NaN] [13] [NaN] [9] [NaN] [15] [NaN] [11] [NaN] [10]
and I need to have> [14] [114] [9] [109] [13] [113] [13] [113] [9] [109] [15] [115] [11] [111] [10]
Thank you in advance.

 채택된 답변

the cyclist
the cyclist 2016년 5월 17일

0 개 추천

Here is one way:
V = [9 nan 14 nan];
idx = find(isnan(V));
V(idx) = V(idx-1) + 100;

댓글 수: 2

the cyclist
the cyclist 2016년 5월 17일
Note that this will break if the first value is NaN, but that seems logical since you would need a different rule for that case.
Rene Sebena
Rene Sebena 2016년 5월 17일
Cool, thank you very much.

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

추가 답변 (1개)

goerk
goerk 2016년 5월 17일

0 개 추천

Very simple solution/idea:
I suppose you have a vector
v = [14 NaN 9 NaN 13 NaN];
you can get a Mask with the positions of the NaN's
mask = isnan(v);
shift this mask one position to the left
maskValue = [mask(2:end) false];
now you can create the new vector
v2 = v;
v2(mask) = v(maskValue)+100;
Attention: Special cases (e.g. two NaN's in a row, or a NaN at the beginning) has to be handled.

댓글 수: 3

goerk
goerk 2016년 5월 17일
The Idea behind the proposed solutions are the same. But the two answers before need less code.
Rene Sebena
Rene Sebena 2016년 5월 17일
Thanks, okay this works, and what if the vector that I want to change, let´s say "v" is located in structure variable let´s say A?
Do you mean something like
A.v = [14 NaN 9 NaN 13 NaN];
?

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

카테고리

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

질문:

2016년 5월 17일

댓글:

2016년 5월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by