How to remove zero columns from array

后端 未结 4 1737
被撕碎了的回忆
被撕碎了的回忆 2020-12-12 00:01

I have an array which looks similar to:

 0     2     3     4     0     0     7     8     0    10
 0    32    44    47     0     0    37    54     0    36
         


        
4条回答
  •  情话喂你
    2020-12-12 00:11

    You could also use sum. Sum over the columns and any column with zeros only will be zeros after the summation as well.

    sum(x,1)
    ans =
    
        0   34   47   51    0    0   44   62    0   46
    

    x(:,sum(x,1)>0)
    
    ans =
    
        2    3    4    7    8   10
       32   44   47   37   54   36
    

提交回复
热议问题