How to Normalize a matrix between 0-1 containing NaNs?

1 回表示 (過去 30 日間)
ML
ML 2016 年 9 月 5 日
回答済み: Stephen23 2016 年 9 月 5 日
Can anyone help me please to normalize a matrix between 0-1 which contains NaNs?
the following makes whole the matrix zero
d = A - min (A(:));
nrmdata = d./max(d(:));
  2 件のコメント
Walter Roberson
Walter Roberson 2016 年 9 月 5 日
I do not seem to be able to reproduce the problem. Could you attach a .mat with your A values?
ML
ML 2016 年 9 月 5 日
the matrix is attached, it contains both inf and NaN.

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

回答 (3 件)

KSSV
KSSV 2016 年 9 月 5 日
編集済み: KSSV 2016 年 9 月 5 日
clc; clear all ;
load A.mat ;
idx = isinf(A) ; % get the positions of inf in A
B = A ; % B as A
B(idx) = 0 ; % repalce inf's in B with 0
% normalize the B matrix
norm_A = (B - min(B(:))) / ( max(B(:)) - min(B(:)) ) ;
norm_A(idx) = inf ; % replace norm_A with inf in previous locations/ if required
or
clc; clear all ;
load A.mat ;
m0 = min(A(isfinite(A(:)))); % get minimum in A
m1 = max(A(isfinite(A(:)))); % get maximum in A
norm_A = (A -m0)/(m1-m0 ) ;

Walter Roberson
Walter Roberson 2016 年 9 月 5 日
You cannot normalize data that contains infinity, not unless you are willing to ignore the infinity.
By definition, any finite real value is a negligible fraction of infinity, so when you normalize against infinity, all finite values become 0.

Stephen23
Stephen23 2016 年 9 月 5 日
Ignoring infinity:
S = load('A.mat');
A = S.A;
idx = isfinite(A);
Amax = max(A(idx));
Amin = min(A(idx));
B = (A-Amin) / (Amax-Amin);
and checking:
>> min(B(isfinite(B)))
ans = 0
>> max(B(isfinite(B)))
ans = 1

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by