how to remove NAN from a double inside a cell array?

can some one how can I remove NAN from a double inside a cell array (attached file)?

5 件のコメント

Guillaume
Guillaume 2020 年 1 月 23 日
Given this matrix:
M = [1 2 3
4 5 6
7 8 NaN
9 10 NaN
11 NaN NaN
NaN NaN NaN]
which is similar to some of your matrices. What do you want as an output once the NaNs have removed the NaNs?
Farshid Daryabor
Farshid Daryabor 2020 年 1 月 23 日
yes, thanks
Image Analyst
Image Analyst 2020 年 1 月 23 日
That's not an answer. Matrices have to be rectangular - they can't have ragged edges. So the NaN's must be replaced by something, not removed. So what do you want to replace them with?
Guillaume
Guillaume 2020 年 1 月 23 日
You haven't answered my question and we can't really answer yours until you do. You may get some answers that do something but possibly not what you want.
Given the matrix M in my example, you could
  • Delete rows that have any NaN, in my example this would also remove the numbers 7, 8, 9 and 10, ending up with:
M = [1 2 3
4 5 6]; %all the other rows have NaN
  • Delete rows that are all NaN, this still leaves some NaN, but doesn't delete any number, ending up with:
M = [1 2 3
4 5 6
7 8 NaN
9 10 NaN
11 NaN NaN];
  • Delete columns that have any NaN, in my example this delete all columns, ending up with
M = []
  • Delete columns that are all NaN. In my example this doesn't delete any rows. You get the same M
  • Delete all the NaNs, since you can't have holes in a matrix, you'd end up with a vector:
M = [1 4 7 9 11 2 5 8 10 3 6] %in this order
  • do something else that you haven't explained.
So, please clarify.
Farshid Daryabor
Farshid Daryabor 2020 年 1 月 23 日
Each columns are corresponds to a latitude and longitude to introduce seawater temperature profiles. I want to keep the matrix structure as is, but delete the NaNs from each column.
I tried to interpolate NaNs, but problem is converting cell to number, I used the following function, encountering with error, Out = cellfun(@cell2mat, (inpaint_nans(mycelldata,0)) ); Any comment to improve.

サインインしてコメントする。

回答 (2 件)

Hamdan Almusaibeli
Hamdan Almusaibeli 2021 年 4 月 7 日

1 投票

>> uq =
1×3 cell array
{3×3 double} {7×3 double} {7×3 double}
>> uq{1}
ans =
0.5000 0.3333 0.0500
2.5000 0.3333 0.0500
2.5000 0.3333 0.0500
NaN NaN NaN
NaN NaN NaN
NaN NaN NaN
NaN NaN NaN
NaN NaN NaN
uq{1}(isnan(uq{1}(:,1)),:) = []
>> uq{1}
ans =
0.5000 0.3333 0.0500
2.5000 0.3333 0.0500
2.5000 0.3333 0.0500
KSSV
KSSV 2020 年 1 月 23 日

0 投票

You can fill the nans using fillmissing. Also you can do interpolation ang the values at the places of nan. Read about interp1.

7 件のコメント

Farshid Daryabor
Farshid Daryabor 2020 年 1 月 23 日
I know by the function inpaint_nans can interploate NaNs but I don want do that , I want to remove.
Farshid Daryabor
Farshid Daryabor 2020 年 1 月 23 日
for i = 1 : length(temp_cmem)
T{i} = inpaint_nans(temp_cmem{1,i},0);
end
KSSV
KSSV 2020 年 1 月 23 日
You want to remove the rows if any element in that row is NaN?
Farshid Daryabor
Farshid Daryabor 2020 年 1 月 23 日
Yes
KSSV
KSSV 2020 年 1 月 23 日
If A is your matrix with Nan's.
iwant = A(~isnan(sum(A,2)),:)
Guillaume
Guillaume 2020 年 1 月 23 日
Note that this is a solution to my second bullet point.
A better way (probably faster) to do the above is:
newA = A(~any(isnan(A), 2), :)
Justin Xuereb
Justin Xuereb 2022 年 4 月 19 日
worked

サインインしてコメントする。

カテゴリ

タグ

質問済み:

2020 年 1 月 23 日

コメント済み:

2022 年 4 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by