Borrar filtros
Borrar filtros

Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

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

1 visualización (últimos 30 días)
Santtu Söderholm
Santtu Söderholm el 24 de Sept. de 2016
Cerrada: MATLAB Answer Bot el 20 de Ag. de 2021
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?

Respuestas (1)

Walter Roberson
Walter Roberson el 24 de Sept. de 2016
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.

La pregunta está cerrada.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by