フィルターのクリア

Info

この質問は閉じられています。 編集または回答するには再度開いてください。

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

1 回表示 (過去 30 日間)
Santtu Söderholm
Santtu Söderholm 2016 年 9 月 24 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
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