Delete NaN values in matrix, but the shape changes

I want to delete NaN values in a matrix that is 320 x 360, which a managed to do by first making a logical
m=isnan(data)
data(m)=[]
but then the matrix data change to 1 x 5890, I don't understand why this is happening ? How do I get the data to have the same number of rows and columns as the original matrix ?

1 件のコメント

Adam
Adam 2017 年 3 月 1 日
What do you expect to be there in place of the NaNs? You can't have emptiness in a numeric array so when you remove values it loses its structure and becomes a 1d array instead.

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

回答 (1 件)

KSSV
KSSV 2017 年 3 月 1 日

0 投票

When you try to remove the Nan's the number of elements in the matrix changes, so MATLAB will put them into array. If you want the same dimension, don'e remove the NaN's, replace them with 0's.
k = rand(3,3) ;
% put nans
k(3,2) = NaN ;
k(3,3) = NaN ;
k(1,3) = NaN ;
% remove nanas
idx = isnan(k) ;
k(idx) = 0

3 件のコメント

AmH
AmH 2017 年 3 月 1 日
okay, because I want to remove or ignore the NaN values. In my matrix these are points that are outside of my area of interest and I want to know how many points there are when the NaNs are not included.
Can I ignore the NaNs ?
Adam
Adam 2017 年 3 月 1 日
nnz( ~isnan( k ) );
KSSV
KSSV 2017 年 3 月 1 日
If you know the coordinates of your region of interest use inpolygon to know the points which lie in, on, out side the region.

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

カテゴリ

タグ

質問済み:

AmH
2017 年 3 月 1 日

コメント済み:

2017 年 3 月 1 日

Community Treasure Hunt

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

Start Hunting!

Translated by