i have the following in mathematica and want to use it in matlab.I tried but i have mistakes and can't fixed them.It is that i don't get yet the matlab philosophy! So,
intMC = {}; sigmat = {};
Do[np1 = np + i*100;
xpoints = Table[RandomReal[], {z1, 1, np1}];
a1t = Table[f[xpoints[[i2]]], {i2, 1, np1}];
a12 = StandardDeviation[a1t]/Sqrt[Length[a1t]];
AppendTo[intMC, {np1, Mean[a1t], a12}];
AppendTo[sigmat, {np1, a12}],
{i, 1, ntr}];
I did this:
fx=@ (x) exp(-x.^2);
i开发者_开发知识库ntmc=zeros();
sigmat=zeros();
for i=1:ntr
np1=np+i*100;
xpoints=randn(1,np1);
for k=1:np1
a1t=fx(xpoints(k))
end %--> until here it prints the results,but in the
%end it gives
% me a message " Attempted to access xpoints(2,:);
%index out of bounds because size(xpoints)=[1,200]
%and stops executing.
%a1t=fx(xpoints(k,:)) % -->I tried this instead of the above but
%a1t=bsxfun(@plus,k,1:ntr) % it doesn't work
a12=std(a1t)/sqrt(length(a1t))
intmc=intmc([np1 mean(a1t) a12],:) %--> i can't handle these 3 and
sigmat=sigmat([np1 a12 ],:) %as i said it stopped executing
end
In order to append a scalar to a Matlab array, you can call either array(end+1) = value
or array = [array;value]
(replace the semicolon with a comma if you want a 1-by-n array). The latter also works for appending arrays; to append arrays with the former, you'd call array(end+1:end:size(newArray,1),:) = newArray
in case you want to catenate along the first dimension.
However, appending in loops that do more than, say, 100 iterations, is a bad idea in Matlab, because it is slow. You're better off pre-assigning the array first - or even better, vectorizing the computation so that you don't have to loop at all.
If I understand you correctly, you want to calculate mean and SEM from a growing number of samples from a normal distribution. Here's how you can do this with a loop:
intmc = zeros(ntr,3); %# stores, on each row, np1, mean, SEM
sigmat = zeros(ntr,2); %# stores, on each row, np1 and SEM
for i=1:ntr
%# draw np+100*i normally distributed random values
np1 = np+i*100;
xpoints = randn(np1,1);
%# if you want to use uniform random values (as in randomreal), use rand
%# Also, you can apply f(x) directly on the array xpoints
%# caculate mean, sem
m = mean(xpoints);
sem = std(xpoints)/sqrt(np1);
%# store
intmc(i,:) = [np1, m, sem];
sigmat(i,:) = [np1,sem];
end %# loop over i
精彩评论