remove rows that contain zeros from cells array in matlab

조회 수: 12 (최근 30일)
Jwana
Jwana 2012년 10월 25일
댓글: gregorio castillo 2017년 1월 8일
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: in the above cells array m, 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?

채택된 답변

Matt Fig
Matt Fig 2012년 10월 25일
m(cellfun(@(x) ~x(1),m(:,1)),:) = []

추가 답변 (3개)

Jan
Jan 2012년 10월 25일
편집: Jan 2012년 10월 25일
If efficiency matters, it should be considered, than cellfun is slow when operating on anonymous functions. Functions handles are better, but the built-in string functions are really fast:
idx = cellfun('isclass', c, 'char');
c = c(idx, :);
What a pitty that TMW hides them in the documentation instead of mentioning their efficiency.

Matt J
Matt J 2012년 10월 25일
편집: Matt J 2012년 10월 25일
I'm assuming the zeros would only appear in the first column (it seems so from your example), but the following is easily modified if not,
idx=cellfun(@(c) isequal(c,0), m(:,1));
m(idx,:)=[];

Azzi Abdelmalek
Azzi Abdelmalek 2012년 10월 25일
out=m(all(cellfun(@any,m),2),:)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by