How to solve this warning: Matrix is close to singular or badly scaled
1 次查看(过去 30 天)
显示 更早的评论
I'm trying to implement a Evolutionary Strategy algorithm (an optimization algorithm). On increasing the size of the parameter nx from 3 onwards, I'm getting warning
nx = 3
Warning: Matrix is singular to working precision.
nx >= 4
Warning: Matrix is close to singular or badly scaled
At this line
alphar{i}(p,m) = alphar{i}(p,m) - 2*pi*(alphar{i}(p,m)/abs(alphar{i}(p,m)));
Complete Code is attached below
0 个评论
采纳的回答
Walter Roberson
2016-11-14
Change
[p,m] = find(abs(alphar{i}) > pi);
alphar{i}(p,m) = alphar{i}(p,m) - 2*pi*(alphar{i}(p,m)/abs(alphar{i}(p,m)));
to
ind = find(abs(alphar{i}) > pi);
alphar{i}(ind) = alphar{i}(ind) - 2*pi*(alphar{i}(ind) ./ abs(alphar{i}(ind)));
Your find is returning multiple locations. That results in a vector of p and a vector of m. When you access alphar{i} with two vectors as coordinates, the result is not the array accessed at just the individual locations (p(K), m(K)) for K = 1 : length(p) : the result is submatrices, exactly like if you had asked for
A([3, 8], [9, 2])
meaning to access the submatrix [A(3,9), A(3,2); A(8, 9), A(8,2)]
Even after you correct for that by using the linear indices instead of the subscript pairs, you have a matrix / a matrix, which is like Array * inv(Array) -- algebraic matrix division. But you want the locations to act independently, so you need the ./ operator not /
0 个评论
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!