在这个Matlab代码中,我试图生成一个int的随机向量并用它们的和除以使其表现得像概率值。但有时(并非总是)它不能总结为1.它是一个浮点精度问题吗? 我该如何解决?
for I = 1:5
A = randi([1,10],[1,10]);
% Just get a 10 length vector of random int
A = A/sum(A);
% Make the vector normalize to
% behave like probability values
if sum(A)~=1
% should sum to one
% but sometimes its not
disp(['Not equal to 1', sum(A)]);
else
disp('Equal to 1');
end
end