i am trying to add the value of a certain function to an array, but there seems to be some error. the code is as follows. could you please help? trying to store the value of Thetahat into A for every x.

1 次查看(过去 30 天)
function [inseed] = optsim (inseed, nreps)
sum = 0;
sum2 = 0;
for x = 0:5:60
for j= 1:nreps
[inseed, profit] = poisprofit(inseed, x);
sum = sum+profit;
sum2 =sum2 + profit^2;
end
Thetahat = sum/nreps;
sigmahat = sqrt(sum2/nreps-Thetahat^2);
sehatThetahat = sigmahat/sqrt(nreps);
A(x)= profit;
D(x) = x;
end
end

回答(1 个)

Walter Roberson
Walter Roberson 2017-2-14
编辑:Walter Roberson 2017-2-14
Only positive integers can be used as indices.
You should use a pattern more like
sum1 = 0;
sum2 = 0;
xvals = 0:5:60;
for xidx = 1 : length(xvals)
x = xvals(xidx);
for j= 1:nreps
[inseed, profit] = poisprofit(inseed, x);
sum1 = sum1+profit;
sum2 =sum2 + profit^2;
end
Thetahat = sum1/nreps;
sigmahat = sqrt(sum2/nreps-Thetahat^2);
sehatThetahat = sigmahat/sqrt(nreps);
A(xidx)= profit;
D(xidx) = x;
end
end

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by