How can I delete NaN cell from a cell array?

1 次查看(过去 30 天)
I have this cell array, how do I get rid of cells that contain NaN?
  10 个评论
dpb
dpb 2020-6-27
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 2020-6-28
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

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by