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
Fangjun Jiang
2020년 6월 26일
utilize isnan()
the cyclist
2020년 6월 26일
편집: the cyclist
2020년 6월 26일
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
2020년 6월 26일
As usual, it really helps if you post a small example showing input and desired output.
Angela Marino
2020년 6월 26일
편집: Angela Marino
2020년 6월 26일
the cyclist
2020년 6월 26일
Hm.
It seems that every row of
array_dati{1}.X
has at least one NaN.
Angela Marino
2020년 6월 26일
Walter Roberson
2020년 6월 26일
Why are you not removing the nan before doing the kmeans?
Angela Marino
2020년 6월 26일
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
2020년 6월 28일
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!