How to find the indices of nonzero rows in a matrix?

前端 未结 3 621
北海茫月
北海茫月 2021-01-20 19:56

How to find the indices of nonzero rows in a matrix?

Example:

A = [
       14  0  6  9  8  17
       85 14  1  3  0  99
       0   0  0  0  0   0 
           


        
相关标签:
3条回答
  • 2021-01-20 20:42

    You can use

       ix = any(x,2);
    

    any check whether there is any element that is not a zero. The second argument stands for "per-row" computation.

    If you want to get the numeric index, you can use find function:

       numIx = find(ix);
    

    Another method:

      ix = sum(abs(x),2)~=0;
    
    0 讨论(0)
  • 2021-01-20 20:46

    Use

    [i,~] = ind2sub(size(A),find(A));
    
    v = unique(i);
    

    Result for the matrix given above:

    v = unique(i')
    
    v =
    
         1     2     4     6
    
    0 讨论(0)
  • 2021-01-20 20:53

    Here's one that ab(uses) the fast matrix multiplication in MATLAB -

    idx = find(abs(A)*ones(size(A,2),1))
    
    0 讨论(0)
提交回复
热议问题