# Replace NaN with mean of previous values

16 views (last 30 days)

Show older comments

Hello, I have a vector that contains some Nan's. How can I replace them with a mean of the previous 5 values? This is what I have tried until now:

for i = 254:1:size(vector)

vector(isnan(vector(i))) = mean(vector(i-5:i-1));

end

and

for i = 254:1:size(vector)

if vector(i) == NaN

vector(i) = mean(vector(i-5:i-1));

end

end

Thank you!

##### 4 Comments

### Accepted Answer

Ameer Hamza
on 10 May 2018

Edited: Ameer Hamza
on 10 May 2018

You can avoid for loop altogether

ind = find(isnan(vector));

vector(ind) = arrayfun(@(x) nanmean(vector(x-5:x-1)), ind);

a vectorized code is usually faster and at worst equivalent to loops. Also using arrayfun() makes your code more readable.

##### 5 Comments

Ameer Hamza
on 11 May 2018

### More Answers (3)

Steven Lord
on 10 May 2018

##### 0 Comments

Jan
on 11 May 2018

Edited: Jan
on 12 May 2018

You forgot to mention, which problem you have with the shown code.

1. size(vector) replies a vector. If vector is a row vector, the output is e.g. [1, 1000]. When this is used in

for i = 254:1:size(vector)

only the first element is used. So this is equivalent to:

for i = 254:1:1

As Fangjun Jiang has suggested already, use numel instead of size.

2. A comparison with NaN is FALSE in every case by definition. See:

NaN == NaN

Therefore

if vector(i) == NaN

must fail. Use this instead:

if isnan(vector(i))

A working version:

for k = 254:numel(vector)

if isnan(vector(k))

vector(k) = mean(vector(k-5:k-1));

end

end

Or

for k = find(isnan(vector))

vector(k) = mean(vector(k-5:k-1));

end

##### 0 Comments

### See Also

### Community Treasure Hunt

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

Start Hunting!