Dismiss Notice
Join Physics Forums Today!
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

  1. Apr 6, 2016 #1
    I have a a multidimensional array (49x49x49) which has NaN values scattered randomly.

    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?


    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?
    Last edited: Apr 6, 2016
  2. jcsd
  3. Apr 6, 2016 #2
    How is the data loaded into the array?
    Could you modify the loading to make the adjustments then?
    Or at least create a list of indexes so you don't have to search through the array again?
  4. Apr 6, 2016 #3
    no sadly not
  5. Apr 6, 2016 #4
    Sadly the only way I know to do it then would be via 3 nested loops. 49*49*49 shouldn't take too long.
  6. Apr 8, 2016 #5


    User Avatar
    Gold Member

    Check out the repnan function on the File Exchange:


    It has several options for interpolation. Notice, however, that it only works on 1-D arrays. You'll want to transform your N-D array into a vector, use repnan, then reshape back into the original size:

    Code (Text):

    B = repnan(A(:));
    A = reshape(B,size(A))
  7. Apr 21, 2016 #6
    What if the first element is a nan? What if a nan is on the an i=1 edge so that there is no i-1 value? This problem can be quickly solved once those questions are addressed.
  8. Apr 26, 2016 #7
    "NaN values scattered randomly."
  9. Apr 26, 2016 #8
    That does not answer my question. In your example you replace one nan by the 7 to its left. You are replaced the other nan with the two to its left. What if the nan is on the left-hand column?
  10. Jun 26, 2016 #9
    Here is a 2-D example:

    % Create sample data
    data = randi(99, 10, 10);
    % Introduce 7 nans at random locations
    data(randperm(numel(data), 7)) = nan
    % Determine location of nans.
    nanMap = isnan(data);
    % Need to get rid of nan's for imtranslate.
    data(nanMap) = -1
    % Make an array shifted one column to the right.
    replacements = imtranslate(data, [1, 0])
    % Initialize a copy of data for our output.
    output = data;
    % Replace nan's with element to the left
    output(nanMap) = replacements(nanMap)
  11. Oct 6, 2016 #10
    vNanIdx = find(isnan(mData));

    mData(vNanIdx) = mData(vNanIdx - 1);

    Remember MATLAB is Column Major, hence if the Previous item is according to the row, permute the array accordingly (Or use something which depends on the dimensions of the array instead 1).

    Pay attention it won't work if the first item is NaN.
  12. Oct 6, 2016 #11


    User Avatar
    Science Advisor
    Gold Member
    2017 Award

    But that is easy to fix as a special case with one line.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted