Info

此问题已关闭。 请重新打开它进行编辑或回答。

Error: Subscript indices must either be real positive integers or logicals

1 次查看(过去 30 天)
The following code produces the error in the title:
syms x;
% Alkuarvoja
n = 100;
P = 0.02;
lambda = n*P; % Poissonin parametri
% Tiheysfunktiot
b(x) = nchoosek(n,x) * P^x * (1-P)^(n-x); % Binomi
p(x) = (lambda^(x))/(exp(-lambda) * factorial(x)); % Poisson
% Todennäköisyydet
Pb = 0; % Binomi
Pp = 0; % Poisson
% Binomilaskenta
for i = 0:1:2
Pb = Pb + b(i);
end
% Poissonin approksimaatio
for i = 0:1:2
Pp = Pp + P(i);
end
Pp = 1 - Pp;
Pb = vpa(Pb);
Pp = vpa(Pp);
disp(Pb)
disp(Pp)
Any idea what might be causing it?

回答(1 个)

Walter Roberson
Walter Roberson 2016-9-24
You have
for i = 0:1:2
Pp = Pp + P(i);
end
MATLAB is case sensitive, so the P there refers to
P = 0.02;
not to
p(x) = (lambda^(x))/(exp(-lambda) * factorial(x)); % Poisson
We recommend avoiding using different variables that differ only in uppercase / lowercase: you can certainly do that and sometimes there are good reasons to do it, but unless there are good reasons the practice is more likely to lead to exactly this kind of confusion.

此问题已关闭。

产品

Community Treasure Hunt

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

Start Hunting!

Translated by