Dealing with NaN's in matlab functions

前端 未结 4 1329
我在风中等你
我在风中等你 2020-12-11 22:18

I was wondering if matlab has a built in way to deal with NaN\'s in function calls. More specifically, I am trying to take the mean of a vector that has a

相关标签:
4条回答
  • 2020-12-11 22:19

    I think this should work:

    mean(x(isfinite(x)));
    
    0 讨论(0)
  • 2020-12-11 22:20

    As of MATLAB 2015a, mean supports an extra parameter, nanflag. Using the example from JoErNanO's answer,

    A = [1 0 NaN; 0 3 4; 0 NaN 2];
    mean(A, 'omitnan')
    % = [0.333333333333333 1.5 3]
    

    The default for that parameter is includenan, which will return NaN for columns/rows containing NaNs.

    median, cov, min, max, sum, var and std also support ignoring of NaNs.

    0 讨论(0)
  • 2020-12-11 22:37

    You could do something like mean(x(~isnan(x))). If you want you could also write a bunch of wrappers like this and put them in your startup.m file.

    0 讨论(0)
  • 2020-12-11 22:46

    What about Matrices?

    As Karthik V suggests,

    mean(x(~isnan(x)))
    

    will work for vectors. However in case you have an n-by-m matrix and wish to compute the row-/column-wise mean discarding occasional NaN's you will have to run a for loop.

    Sample Scenario

    Imagine a data matrix of the form:

    A = [1 0 NaN; 0 3 4; 0 NaN 2]
    
    A =
     1     0   NaN
     0     3     4
     0   NaN     2
    

    Running mean(A(~isnan(A))) yields:

    ans =
    
    1.4286
    

    This is because the logical indexing effectively "flattens" the matrix into a vector.

    Looping Solution (Column-wise Mean)

    Assuming you want to compute the column-wise mean, the looping solution then becomes:

    % Preallocate resulting mean vector
    nCols = size(A, 2);
    mu = zeros(1, nCols);
    
    % Compute means
    for col = 1:nCols
        mu(col) = mean(A(~isnan(A(:, col)), col));
    end
    

    Resulting in:

    mu =
    
    0.3333    1.5000    3.0000
    

    Looping Solution (Row-wise Mean)

    Assuming you want to compute the row-wise mean, the looping solution then becomes:

    % Preallocate resulting mean vector
    nRows = size(A, 1);
    mu = zeros(nRows, 1);
    
    % Compute means
    for row = 1:nRows
        mu(row) = mean(A(row, ~isnan(A(row, :))));
    end
    

    Resulting in:

    mu =
    
    0.5000
    2.3333
    1.0000
    
    0 讨论(0)
提交回复
热议问题