in matlab delete an entire row if the elements in certain columns equal zero

前端 未结 3 1455
谎友^
谎友^ 2021-01-27 14:07

I matlab I have a matrix of 4 columns. I want to delete an entire row if the elements in columns two, three and four all equal zero.

So, I think I need to do something l

相关标签:
3条回答
  • 2021-01-27 14:33

    You can do it like that:

    a(sum((a(:,2:4)==0)')==3,:)=[]
    
    0 讨论(0)
  • 2021-01-27 14:40

    You can use logical indexing + any

    data = data(any(data(:,2:4),2),:);
    

    Here instead of deleting rows we preserve a row if the conditions is not satisfied for it.

    0 讨论(0)
  • 2021-01-27 14:43
    % toy example
    >> a = [1,0,0,1; 
            2,0,2,0; 
            3,0,0,0; 
            4,4,0,4; 
            5,0,0,0; 
            6,0,0,0; 
            7,0,7,0; 
            8,0,0,8; 
            9,0,0,0]; 
    
    % solution
    >> a(sum ( a(:, 2:4) == 0, 2) == 3, :) = []
    a =
       1   0   0   1
       2   0   2   0
       4   4   0   4
       7   0   7   0
       8   0   0   8
    

    I.e. if the sum of the number of zeros across columns 2 to 4 equals 3, delete that row.
    (i.e. use logical indexing)

    0 讨论(0)
提交回复
热议问题