How to remove zeros from an element in a cell array
显示 更早的评论
Hi all,
I have two cell arrays each contain 12 cells, each cell contains different matrix size (31,1) or (30,1) or (28,1), each cell in the array contains a bunch of zeros, and I need to remove these zeros, because it indicates that there is no data at this day
here are an example of my data,
A = {[1 0 1], [2 0 2], ......., [12 0 12]}
B = {[0 2 1], [1 2 0], ........, [12 0 12]}
my purpose is to compare A with B, and to do that I have to remove the zeros and the coincident index in the other cell
A(1,1){2} and B(1,1){2} = []; aslo A(1,1){1} and B(1,1){1}=[];
any suggestions, please??
采纳的回答
更多回答(1 个)
dpb
2021-7-11
yourarray=cellfun(@(c)c(~-0),yourarray,'UniformOutput',false);
4 个评论
Walter Roberson
2021-7-11
Pretty sure the ~-0 is a typo ;-)
Ebtesam Farid
2021-7-11
编辑:Ebtesam Farid
2021-7-11
dpb
2021-7-12
Au contraire --
>> c{:}
ans =
5
3
1
3
0
ans =
2
5
2
1
1
>>
>> c=cellfun(@(c)c(c~=0),c,'UniformOutput',false)
c =
1×2 cell array
{4×1 double} {5×1 double}
>> c{:}
ans =
5
3
1
3
ans =
2
5
2
1
1
>>
Walter Roberson
2021-7-12
Yes, but this is different code; your posted code had c(~-0) not c(c~=0)
类别
在 帮助中心 和 File Exchange 中查找有关 Coordinate Reference Systems 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!