# Replacing NaN with value in previous column

1 Ansicht (letzte 30 Tage)
Rene Sebena am 17 Mai 2016
Kommentiert: goerk am 19 Mai 2016
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]
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

the cyclist am 17 Mai 2016
Here is one way:
V = [9 nan 14 nan];
idx = find(isnan(V));
V(idx) = V(idx-1) + 100;
##### 2 KommentareKeine anzeigenKeine ausblenden
the cyclist am 17 Mai 2016
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 am 17 Mai 2016
Cool, thank you very much.

Melden Sie sich an, um zu kommentieren.

### Weitere Antworten (1)

goerk am 17 Mai 2016
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
shift this mask one position to the left
now you can create the new vector
v2 = v;
Attention: Special cases (e.g. two NaN's in a row, or a NaN at the beginning) has to be handled.
##### 3 Kommentare1 älteren Kommentar anzeigen1 älteren Kommentar ausblenden
Rene Sebena am 17 Mai 2016
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?
goerk am 19 Mai 2016
Do you mean something like
A.v = [14 NaN 9 NaN 13 NaN];
?

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Logical finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by