Why NaN even though all elements considered

1 回表示 (過去 30 日間)
Abhishek H P
Abhishek H P 2016 年 5 月 25 日
編集済み: Stephen23 2016 年 5 月 25 日
I am comparing every column with other columns but getting the answer in R(4,2) and R(3,1)
for i=1:nrow-1
a=data(1,i);
c=data(2,i);
e=data(3,i);
g=data(4,i);
for j=[1:i-1,i+1:nrow]
b=a-data(1,j);
d=c-data(2,j);
f=e-data(3,j);
h=g-data(4,j);
kU=b'*b;
kL=d'*d;
pU=f'*f;
pL=h'*h;
S=(kU'*kU+kL'*kL)/(kU+kL)+(pU'*pU+pL'*pL)/(pU+pL);
R(i,j) = S;
R(j,i) = S;
end
end
end
trial(A)
ans =
0 1.8299 NaN 1.8299
1.8299 0 1.8299 NaN
NaN 1.8299 0 1.8299
1.8299 NaN 1.8299 0
when the input
A = 2.0000 2.2000 2.0000 2.2000
3.0000 3.1000 3.0000 3.1000
2.0000 2.2000 2.0000 2.2000
3.0000 3.1000 3.0000 3.1000
  2 件のコメント
Stephen23
Stephen23 2016 年 5 月 25 日
編集済み: Stephen23 2016 年 5 月 25 日
Your algorithm divides zero by zero. This happens for these pairs of (i,j): (1,3), (2,4), (3,1). What do you expect division of zero by zero to do?
Roger Stafford
Roger Stafford 2016 年 5 月 25 日
Actually it is zero divided by zero that is producing the NaNs here. A non-zero divided by zero will produce inf or -inf,

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

回答 (0 件)

カテゴリ

Help Center および File ExchangePerformance and Memory についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by