How can I delete NaN cell from a cell array?

3 views (last 30 days)
I have this cell array, how do I get rid of cells that contain NaN?
  10 Comments
dpb
dpb on 27 Jun 2020
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 on 28 Jun 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

Sign in to comment.

Answers (0)

Categories

Find more on Creating and Concatenating Matrices 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!