## NAN answers problem in matrix

### marc (view profile)

on 22 May 2019
Latest activity Commented on by marc

### marc (view profile)

on 22 May 2019

Hey everyone. I wanted to calculate the distance matrix of "L" but answers are all NANs.what is the problem?
sc=zeros(1000,72);
%% WT :
V_ci=4;
V_r= 12;
V_co=25;
Nwt=25;
Pwr=2*Nwt;
for i=1:1000
for c=1:24
m = [9.9,9.3667,9.1667,9,8.7,8.6,9,9.0333,9.3333,9.6,10.1333,10.2667,7.9667,8,8,7.7333,6.9667,5.9667,4.8333,4.4333,4.3333,4.1000,4.0667,4];
sigma = [0.7937,0.8021,0.8505,0.8185,0.755,1.0583,1.1533,1.1504,0.9504,1.1533,1.0066,0.8622,0.3786,0.4583,0.5,0.4509,0.2309,0.3786,0.3215,0.3215,0.4163,0.2646,0.2082,0.1732];
m_v=m(c);
sigma_v=sigma(c);
shape=(sigma_v/m_v)^(-1.086);
scale=m_v/gamma(1+1/shape);
sc(i,c)=wblrnd(scale,shape);
end
end
%% PV :
Npv=100000;
PF_pv=1;
for i=1:1000
for d=25:48
mu=[zeros(1,24),0,0,0,0,0.032,0.1278,0.2538,0.3824,0.4908,0.5680,0.6164,0.5990,0.5614,0.4672,0.3548,0.2228,0.1030,0,0,0,0,0,0,0];
sd=[zeros(1,24),0,0,0,0,0.0045,0.0406,0.0714,0.1189,0.1388,0.1659,0.1445,0.1175,0.0995,0.0788,0.0550,0.0410,0.0276,0,0,0,0,0,0,0];
mu_pv=mu(d);
sd_pv=sd(d);
beta=(1-mu_pv)*(mu_pv*(1-mu_pv)/sd_pv^2-1);
alfa=mu_pv*beta/(1-mu_pv);
sc(i,d)=betarnd(alfa,beta);
end
end
for i=1:1000
for f=49:72
sc(i,f)=0.9+0.2*betarnd(2,2);
end
end
M=zeros(1,72);
M = max(sc,[],1); % finding maximum array in each column
L=zeros(1000,72);
b=1;
for i=1:1000
for t=1:72
L(i,t)=sc(i,t)/M(b,t);
end
end
%% distance
N = size(L,1);
q(N,N) = 0;
for i = 1:(N-1)
X = ones((N-i),1)*L(i,:);
q((i+1):N,i) = sqrt( sum((X - L((i+1):N,:)).^2,2) );
end
D =q + q';

### Tags

on 22 May 2019

on 22 May 2019

First, two take-home messages
• 0/0 = nan
• Once you have a NaN in your data, it potentially spreads like wildfire
The NaN is first appearing in your 2nd i-for-loop in this line below because you're dividing zero by zero (which equals NaN) and then multiplying NaN by whatever which also equals NaN. So beta equals NaN.
beta=(1-mu_pv)*(mu_pv*(1-mu_pv)/sd_pv^2-1);
% |_____0________/__0__|
By the time you are out of those nested loops, you've got 11000 NaNs in your sc array.
sum(isnan(sc(:))) % = 11000
Since subsequent lines of code uses sc, those NaNs propagate through the rest of your code.

marc

### marc (view profile)

on 22 May 2019
thank you so much for your answer. But I shoud have those zeros in my data,what should I do to solve it?

on 22 May 2019
To avoid those NaNs, you need to avoid dividing zero by zero (0/0).
mu=[zeros(1,24),0,0,0,0,0.032,0.1278,0.2538,0.3824,0.4908,0.5680,0.6164,0.5990,0.5614,0.4672,0.3548,0.2228,0.1030,0,0,0,0,0,0,0];
sd=[zeros(1,24),0,0,0,0,0.0045,0.0406,0.0714,0.1189,0.1388,0.1659,0.1445,0.1175,0.0995,0.0788,0.0550,0.0410,0.0276,0,0,0,0,0,0,0];
These vectors (above) contain 0s and your code is using those zeros. I have no idea what those variables represent or why you're dividing them. It's up to you to figure that out. Maybe the NaNs should be there or maybe they're a sign that something's wrong.
There are lots of inefficiencies in your code including
• hard coded variables such as m = [9.9,9.3667...]; it would be better to load that data from its source rather than hard coding it.
• Many parts of your for-loops (or perhaps all of the for-loops) can be replaced with vectorization.
• Instead of looping from d=25 to d=48 and then offsetting the vectors with 24 zeros, why not just loop from 1 to 23?
In any case, I recommend stepping through your code, line by line, asking yourself the purpose of each line of code and whether it is serving its purpose correctly.
marc

on 22 May 2019