I have a a multidimensional array (49x49x49) which has NaN values scattered randomly.(adsbygoogle = window.adsbygoogle || []).push({});

For each NaN, I want to replace the value with the previous non NaN value.

e.g. if i had a 3 x 3 matrix

4 7 9

2 7 NaN

1 2 NaN

should become

4 7 9

2 7 7

1 2 2

I have 3 dimensions [i,j,k], but it doesn't matter which dimension I move back in, so long as it is an adjacent element.

Anybody have a solution to my problem?

EDIT:

I guess I could make several loops, find the NaNs and replace e.g i with i-1. But shouldn't there be a more efficient way in Matlab?

**Physics Forums - The Fusion of Science and Community**

Join Physics Forums Today!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# Matlab Replace NaN with next element in Multidimensional Array

Have something to add?

Draft saved
Draft deleted

Loading...

Similar Threads - Replace next element | Date |
---|---|

How do I extract elements of a random matrix masked by a circle with its center at A(15,26) | Sep 15, 2016 |

[Matlab] Replace pixels of an image with theta of Polar Coordinates | Apr 15, 2014 |

Mathematica : Noncommutative Multiply and Replacement Rules | Apr 4, 2014 |

Streamlining Replace in Mathematica 8 | May 31, 2012 |

Mathematica Pattern matching and replace repeated | Apr 22, 2012 |

**Physics Forums - The Fusion of Science and Community**