MATLAB: How To Efficiently Remove NaN Elements from Matrix

后端 未结 5 1047
猫巷女王i
猫巷女王i 2020-12-05 01:13

I am looking for a way to remove the NaN numbers from a matrix in MATLAB efficiently (i.e. without using a for loop)

I will provide a quick example to illustrate wha

相关标签:
5条回答
  • 2020-12-05 01:21

    The best way is

    M(any(isnan(M),2),:)=[]
    

    which will remove any row that contains at least one NaN.

    0 讨论(0)
  • 2020-12-05 01:21

    try my snip function. I wanted to address typical questions like this in one simple function:

    B = snip(A,nan)
    

    you can find the function file at

    It also works with all other 'x', '0' or whatever elements and takes care of more similar problems.

    0 讨论(0)
  • 2020-12-05 01:25

    The following function removes NAN from the data for specified dimensions:

    function data_out = remove_nan (data_in, remove_dim)
    %remove row or col from the data_in if there is a NaN element
    
    % e.g., data_in =[1 2 3 4 NaN; 1 2 3 4 5; 1 2 3 NaN NaN]
    % from this data remove col 4 and 5 such that data_out=[ 1 2 3; 1 2 3; 1 2
    % 3]
    
    if nargin==1
    
        col_loc=any(isnan(data_in),1);
        data_in(:,col_loc)=[];
        data_out=data_in;
    
    elseif nargin==2
    
        if remove_dim=='col'
            %find the cols with nan and remove the colums
            col_loc=any(isnan(data_in),1);
            data_in(:,col_loc)=[];
            data_out=data_in;
        elseif  remove_dim='row'
            %find the rows with nan and remove the rows
            row_loc=any(isnan(data_in),2);
            data_in(row_loc,:)=[];
            data_out=data_in;
        end
    else
        error( 'incorrect no of arguments')
    
    end
    
    0 讨论(0)
  • 2020-12-05 01:36

    If you have either no NaNs or all NaNs on each row, you can do the removal using:

    M(isfinite(M(:, 1)), :)
    
    0 讨论(0)
  • 2020-12-05 01:38

    Actually I would like to recommend a slightly different (and more general) approach.

    So, in case that you want to ignore (i.e. delete) all the rows where at least one column includes NaN, then just:

    M= M(0== sum(isnan(M), 2), :)
    
    0 讨论(0)
提交回复
热议问题