i want to delete cells that contain 0's

1 次查看(过去 30 天)
In a cell array, there are rows of cells that contain 0's. I want to delete these rows.
Thank you.

采纳的回答

Kevin Holly
Kevin Holly 2021-11-24
Assuming the values in cell array are numeric:
cellarray = {34 , 23, 4, 123, 0 ,423, 4312;34 , 23, 4, 123, 5 ,423, 4312;34 , 0, 0, 123, 5 ,423, 4312;34 , 73, 5, 13, 7 ,23, 43;34 , 22, 3, 125, 5 ,423, 4312}
cellarray = 5×7 cell array
{[34]} {[23]} {[4]} {[123]} {[0]} {[423]} {[4312]} {[34]} {[23]} {[4]} {[123]} {[5]} {[423]} {[4312]} {[34]} {[ 0]} {[0]} {[123]} {[5]} {[423]} {[4312]} {[34]} {[73]} {[5]} {[ 13]} {[7]} {[ 23]} {[ 43]} {[34]} {[22]} {[3]} {[125]} {[5]} {[423]} {[4312]}
out = cellfun(@(x) find(x == 0),cellarray,'un',0);
idx = cellfun('isempty',out);
[row, column] = find(~idx);
cellarray(row,:)=[]
cellarray = 3×7 cell array
{[34]} {[23]} {[4]} {[123]} {[5]} {[423]} {[4312]} {[34]} {[73]} {[5]} {[ 13]} {[7]} {[ 23]} {[ 43]} {[34]} {[22]} {[3]} {[125]} {[5]} {[423]} {[4312]}
  3 个评论
Kevin Holly
Kevin Holly 2021-11-24
cellarray = {34 , 23, "ok", 123, 0 ,423, 4312;34 , 23, 4, 123, 5 ,"Words", 4312;34 , 0, 0, 123, 5 ,423, 4312;34 , 73, 5, 13, 7 ,"23", 43;34 , 22, 3, "125", 5 ,423, 4312}
cellarray = 5×7 cell array
{[34]} {[23]} {["ok"]} {[ 123]} {[0]} {[ 423]} {[4312]} {[34]} {[23]} {[ 4]} {[ 123]} {[5]} {["Words"]} {[4312]} {[34]} {[ 0]} {[ 0]} {[ 123]} {[5]} {[ 423]} {[4312]} {[34]} {[73]} {[ 5]} {[ 13]} {[7]} {["23" ]} {[ 43]} {[34]} {[22]} {[ 3]} {["125"]} {[5]} {[ 423]} {[4312]}
out = cellfun(@(x) isnumeric(x) && x==0 ,cellarray,'un',0)
out = 5×7 cell array
{[0]} {[0]} {[0]} {[0]} {[1]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[1]} {[1]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]}
[row, column] = find(cell2mat(out))
row = 3×1
3 3 1
column = 3×1
2 3 5
cellarray(row,:)=[]
cellarray = 3×7 cell array
{[34]} {[23]} {[4]} {[ 123]} {[5]} {["Words"]} {[4312]} {[34]} {[73]} {[5]} {[ 13]} {[7]} {["23" ]} {[ 43]} {[34]} {[22]} {[3]} {["125"]} {[5]} {[ 423]} {[4312]}

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by