MATLAB: Warning: Matrix is singular

badly scaledmatrixnanrcondsingularwarning

Hi,
When i run the code, i get this warning
Matrix is singular, close to singular or badly scaled. Results may be inaccurate.
RCOND = NaN.
and the rest matrices v,var have inf values.
The matrix where i take this warning is 9×9. I attach part of the code to see better where is my fault.
for i=1:num_exp,
distance_hat=distance.*10.^(0.05*sqrt(sigma)*randn(1,N));
m = 2.3026*log10(distance_hat);
var = exp(4*m)*(exp(8*sigma.^2)-exp(4*sigma.^2));
v = var(1,1)+var(1,2:N);
S = eye(N-1).*diag(v) + ~eye(N-1).*var(1,1)
S12 = inv(S)
sol = loc(x,distance_hat,S12);
weighted = loc(x,distance_hat,S12);
end
what should i change to not take this warning?

Best Answer

I guess that the line, which causes the error, is:
S12 = inv(S)
(Please post the complete error message - most of all it is inefficient to let the readers guess, where the error appears.) Then the message means, that there is no inverse of S. Because we do not know, why you create S in the shown way, there is no chance to suggest an improvement.
Related Question