MATLAB Answers

delete rows in a cell array

27 views (last 30 days)
having a cell array containing in 1st column a name of a country and in next column values, how can I delete rows containing 1 or more zeros values?

  1 Comment

Stephen Cobeldick
Stephen Cobeldick on 29 Jan 2019
@Anastasia Anastasiadou: please upload your data in a .mat file by clicking the paperclip button.

Sign in to comment.

Accepted Answer

madhan ravi
madhan ravi on 29 Jan 2019
Edited: madhan ravi on 29 Jan 2019
C={'country1' 11 33 55 40
'country2' 22 0 0 0
'country3' 58 44 55 5 };
idx=cellfun(@all,C,'un',0);
C(~all(cell2mat(idx),2),:)=[]

  10 Comments

Show 7 older comments
madhan ravi
madhan ravi on 29 Jan 2019
Ok try this:
idx=cellfun(@str2double,C(:,2:end),'un',0); % where C is your cell array
C(~all(cell2mat(idx),2),:)=[]
Anastasia Anastasiadou
Anastasia Anastasiadou on 29 Jan 2019
yeeeees! it worked!!!
thank you so muchh
madhan ravi
madhan ravi on 29 Jan 2019
Help us to help you! , that's why we ask you to upload the required files at he very beginning when you ask a question so that a fast solution can be provided.

Sign in to comment.

More Answers (2)

Omer Yasin Birey
Omer Yasin Birey on 29 Jan 2019
Edited: Omer Yasin Birey on 29 Jan 2019
Lets say your cell array's variable name is 'a'
a = a(all(cellfun(@(x)x~=0,a),2),:);

  5 Comments

Show 2 older comments
Omer Yasin Birey
Omer Yasin Birey on 29 Jan 2019
That should work
remZeros = a(all(cellfun(@(x)x~=0,a),2),:);
remZeros(2:end+1,:) = remZeros(1:end,:);
remZeros(1,:) = a(1,:);
Anastasia Anastasiadou
Anastasia Anastasiadou on 29 Jan 2019
it does not.
Error using cellfun
Non-scalar in Uniform output, at index 1, output 1.
Set 'UniformOutput' to false.
and although I set uniformoutput to false again nothing

Sign in to comment.


KSSV
KSSV on 29 Jan 2019
Let A be your matrix with size (m,n)
idx = A==0 ;
A(sum(A,2)~=n,:) = [] ;

  2 Comments

madhan ravi
madhan ravi on 29 Jan 2019
? OP has mentioned it’s a cell array not a matrix.
Anastasia Anastasiadou
Anastasia Anastasiadou on 29 Jan 2019
I have a cell array so the == does not work ! :)

Sign in to comment.

Sign in to answer this question.

Tags