简体   繁体   English

从matlab中的单元格数组中删除包含零的行

[英]remove rows that contain zeros from cells array in matlab

I have a cell array resulted from a certain code as follows: 我有一个由某个代码产生的单元格数组如下:

m = 

    [         0]    'GO:0008150'
    'GO:0008150'    'GO:0016740'
    'GO:0016740'    'GO:0016787'
    'GO:0016787'    'GO:0006810'
    'GO:0008150'    'GO:0006412'
    'GO:0016740'    'GO:0004672'
    'GO:0016740'    'GO:0016779'
    'GO:0016787'    'GO:0004386'
    'GO:0016787'    'GO:0003774'
    'GO:0016787'    'GO:0016298'
    'GO:0006810'    'GO:0016192'
    'GO:0006412'    'GO:0005215'
    'GO:0004672'    'GO:0030533'
    [         0]    'GO:0008150'
    [         0]    'GO:0016740'
    'GO:0008150'    'GO:0016787'
    'GO:0008150'    'GO:0006810'
    'GO:0006810'    'GO:0006412'
    [         0]    'GO:0004672'
    [         0]    'GO:0016779'
    [         0]    'GO:0004386'
    'GO:0016192'    'GO:0003774'
    [         0]    'GO:0016298'
    [         0]    'GO:0016192'
    'GO:0006810'    'GO:0005215'
    'GO:0005215'    'GO:0030533'

I need to remove the rows which contains zero (for example: row one should be deleted because we have a zero in the first column). 我需要删除包含零的行(例如:第一行应该被删除,因为我们在第一列中有一个零)。 so how can I create an array from this array that doesn't contain zeros? 那么如何从这个不包含零的数组创建一个数组呢?

You can do this in a pretty one-liner: 你可以在漂亮的单行中做到这一点:

m(any(cellfun(@(x)x(1)==0, m),2), :) = []

Alternatively: 或者:

m(any(~cellfun(@ischar, m),2), :) = []

which is a tad faster. 这有点快。

If you can be certain that only the first column will ever contain a zero, use 如果您可以确定只有第一列将包含零,请使用

m = m(cellfun(@ischar, m(:,1)),:)

and, finally, you can use 最后,你可以使用

m = m(cellfun('isclass', m(:,1), 'char'),:)

which looks "old", but actually has greater performance. 看起来“老”,但实际上有更大的表现。

Testing these one thousand times on your example array, gives 在您的示例数组上测试这一千次,给出

Elapsed time is 1.382801 seconds.
Elapsed time is 0.138519 seconds.
Elapsed time is 0.075245 seconds.
Elapsed time is 0.014674 seconds.
  zerosLocation = cellfun(@(x)isEqual(x, 0 ) , m);
  zeroRows = any(zerosLocation,2);
  m(zeroRows,:) = [];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM