How to remove zeros from an element in a cell array
Show older comments
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??
Accepted Answer
More Answers (1)
dpb
on 11 Jul 2021
yourarray=cellfun(@(c)c(~-0),yourarray,'UniformOutput',false);
4 Comments
Walter Roberson
on 11 Jul 2021
Pretty sure the ~-0 is a typo ;-)
Ebtesam Farid
on 11 Jul 2021
Edited: Ebtesam Farid
on 11 Jul 2021
dpb
on 12 Jul 2021
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
on 12 Jul 2021
Yes, but this is different code; your posted code had c(~-0) not c(c~=0)
Categories
Find more on Coordinate Reference Systems in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!