How to remove zeros from an element in a cell array
Mostrar comentarios más antiguos
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??
Respuesta aceptada
Más respuestas (1)
dpb
el 11 de Jul. de 2021
yourarray=cellfun(@(c)c(~-0),yourarray,'UniformOutput',false);
4 comentarios
Walter Roberson
el 11 de Jul. de 2021
Pretty sure the ~-0 is a typo ;-)
Ebtesam Farid
el 11 de Jul. de 2021
Editada: Ebtesam Farid
el 11 de Jul. de 2021
dpb
el 12 de Jul. de 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
el 12 de Jul. de 2021
Yes, but this is different code; your posted code had c(~-0) not c(c~=0)
Categorías
Más información sobre Coordinate Reference Systems en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!