How can I delete NaN cell from a cell array?

I have this cell array, how do I get rid of cells that contain NaN?

10 comentarios

Fangjun Jiang
Fangjun Jiang el 26 de Jun. de 2020
utilize isnan()
the cyclist
the cyclist el 26 de Jun. de 2020
Editada: the cyclist el 26 de Jun. de 2020
Can you please help us help you, and give a more detailed description of exactly what you want?
For example, I see that
array_dati{1}.X(5000,1) = NaN NaN 2512.35067448876 NaN
Should we get rid of that entire row?
Should we get rid of that row for all other variables in array_dati{1}?
Should we get rid of that row for all other variables for all other elements in array_dati?
Can we assume that if there is a NaN in one variable (in one structure of one cell), all the corresponding variables in all structures of all cells will have a NaN?
James Tursa
James Tursa el 26 de Jun. de 2020
As usual, it really helps if you post a small example showing input and desired output.
Angela Marino
Angela Marino el 26 de Jun. de 2020
Editada: Angela Marino el 26 de Jun. de 2020
Yes, I'll explain better.
I need to apply the kmeans function and for this reason have data without NaN, I don't know what could be the right way to not have problems due to the presence of NaN values. And yes, we can assume that if there is a NaN in one variable (in one structure of one cell), all the corresponding variables in all structures of all cells will have a NaN.
Hm.
It seems that every row of
array_dati{1}.X
has at least one NaN.
Angela Marino
Angela Marino el 26 de Jun. de 2020
Yes, each "data_array {} ._" has the same number of NaN in the same positions
Walter Roberson
Walter Roberson el 26 de Jun. de 2020
Why are you not removing the nan before doing the kmeans?
Angela Marino
Angela Marino el 26 de Jun. de 2020
I'm trying but I can't find the command that allows me to do it for all the variables of the cell array simultaneously
I think you've got a real problem here...
>> array_dati{1}
ans =
struct with fields:
X: [7200×4 double]
Y: [7200×4 double]
lat: [7200×4 double]
lon: [7200×4 double]
per: [7200×4 double]
v: [7200×4 double]
dist: [7200×4 double]
sim: 0
>>
>> sum(any(isnan(array_dati{1}.X),2))
sum(any(isnan(array_dati{1}.Y),2))
sum(any(isnan(array_dati{1}.lat),2))
sum(any(isnan(array_dati{1}.lon),2))
sum(any(isnan(array_dati{1}.per),2))
sum(any(isnan(array_dati{1}.v),2))
sum(any(isnan(array_dati{1}.dist),2))
sum(all(isnan(array_dati{1}.X),2))
sum(all(isnan(array_dati{1}.Y),2))
ans =
7200.00
ans =
7200.00
ans =
7200.00
ans =
7200.00
ans =
7200.00
ans =
7200.00
ans =
7200.00
ans =
2626.00
ans =
2626.00
>>
Every row has at least one NaN so if you delete the observation because one variable is missing in the row, then you have nothing left.
>> sum(all(isnan(array_dati{1}.Y),2))
ans =
4521.00 4524.00 4523.00 4525.00
>>
There are 4520+/- rows that are nothing but NaN; that leaves 7200-4520 --> ~2680 with at least one observation.
I didn't count the distribution of number of finite by row.
I also don't know otomh the ramification of trying kmeans with missing variables nor how the MATLAB routine handles it.
But, it's simple-enough to eliminate the all NaN rows in favor of keeping those with any--
>> array_dati{1}.X=array_dati{1}.X(any(isfinite(array_dati{1}.X),2),:);
>> array_dati{1}
ans =
struct with fields:
X: [4574×4 double]
Y: [7200×4 double]
lat: [7200×4 double]
lon: [7200×4 double]
per: [7200×4 double]
v: [7200×4 double]
dist: [7200×4 double]
sim: 0
>>
Perhaps the choice would be to select finite observations from the columns of the array and ignore the rows? We don't know what any of it is or means, so it's pretty-much an "anything goes!" approach.
Angela Marino
Angela Marino el 28 de Jun. de 2020
Thanks a lot to everyone for the help! In my cell array I have real data of a bus moving on my road network for this reason I have NaN which represents the moments of time when the bus I am observing is in my network. In any case, I have to think carefully about how to operate. thank you

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Data Type Identification en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 26 de Jun. de 2020

Comentada:

el 28 de Jun. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by