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 件のコメント

Stephen23
Stephen23 2019 年 1 月 29 日
@Anastasia Anastasiadou: please upload your data in a .mat file by clicking the paperclip button.

サインインしてコメントする。

 採用された回答

madhan ravi
madhan ravi 2019 年 1 月 29 日
編集済み: madhan ravi 2019 年 1 月 29 日

0 投票

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 件のコメント

Anastasia Anastasiadou
Anastasia Anastasiadou 2019 年 1 月 29 日
in this case nothing changes as idx is a matrix with all 1
madhan ravi
madhan ravi 2019 年 1 月 29 日
編集済み: madhan ravi 2019 年 1 月 29 日
What? Still if your not satisfied then attach your data as .mat file as Stephen mentioned , I get the below result:
C =
2×5 cell array
{'country1'} {[11]} {[33]} {[55]} {[40]}
{'country3'} {[58]} {[44]} {[55]} {[ 5]}
madhan ravi
madhan ravi 2019 年 1 月 29 日
Anastasia Anastasiadou's answer moved here:
ok. thanks. in my cell does not work as it returns back the same cell with zeros again.
madhan ravi
madhan ravi 2019 年 1 月 29 日
Will you attach your data or not?
madhan ravi
madhan ravi 2019 年 1 月 29 日
Anastasia Anastasiadou's answer moved here:
it is not possible to attach my code as the cell is created from a tsv file. I will try to understand the error, as the idx matrix contains only 1.
thank you
madhan ravi
madhan ravi 2019 年 1 月 29 日
post a picture of your command window when you try my answer remove all the semicolons ;
madhan ravi
madhan ravi 2019 年 1 月 29 日
Anastasia Anastasiadou's answer moved here:
madhan ravi
madhan ravi 2019 年 1 月 29 日
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 2019 年 1 月 29 日
編集済み: madhan ravi 2019 年 1 月 30 日
yeeeees! it worked!!!
thank you so muchh
madhan ravi
madhan ravi 2019 年 1 月 29 日
編集済み: madhan ravi 2019 年 1 月 29 日
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.

サインインしてコメントする。

その他の回答 (2 件)

Omer Yasin Birey
Omer Yasin Birey 2019 年 1 月 29 日
編集済み: Omer Yasin Birey 2019 年 1 月 29 日

0 投票

Lets say your cell array's variable name is 'a'
a = a(all(cellfun(@(x)x~=0,a),2),:);

5 件のコメント

Anastasia Anastasiadou
Anastasia Anastasiadou 2019 年 1 月 29 日
sorry, i forgot to mention that I want to check from the 2nd column till the end. so lets assume that I have a cell 30*13 with element
country 11 33 55 40
country2 22 0 0 0
country3 58 44 55 5
I would like to delete the rows that contain zeros.
Sorry for the simple question but it is the first attempt with matlab :)
madhan ravi
madhan ravi 2019 年 1 月 29 日
you mean if there is any zero in the row you want to delete it?
Anastasia Anastasiadou
Anastasia Anastasiadou 2019 年 1 月 29 日
yes !
Omer Yasin Birey
Omer Yasin Birey 2019 年 1 月 29 日
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 2019 年 1 月 29 日
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

サインインしてコメントする。

KSSV
KSSV 2019 年 1 月 29 日

0 投票

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

2 件のコメント

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

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by