从mathematica 到matlab 的转换 --> (附录)
我在mathematica中有以下内容,想在matlab中使用它。我尝试过,但我有错误并且无法修复它们。这是我还不明白matlab哲学! 所以,
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}];
我这样做了:
fx=@ (x) exp(-x.^2);
intmc=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
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);
intmc=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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了将标量附加到 Matlab 数组,您可以调用
array(end+1) = value
或array = [array;value]
(将分号替换为如果你想要一个 1×n 数组,则用逗号)。后者也适用于追加数组;要将数组附加到前者,您可以调用 array(end+1:end:size(newArray,1),:) = newArray 以防您想沿第一个维度进行串联。然而,在 Matlab 中,追加执行超过 100 次迭代的循环是一个坏主意,因为它很慢。您最好首先预先分配数组 - 或者甚至更好,对计算进行矢量化,以便您根本不必循环。
如果我理解正确的话,您想从正态分布中越来越多的样本中计算平均值和 SEM。以下是如何使用循环执行此操作:
In order to append a scalar to a Matlab array, you can call either
array(end+1) = value
orarray = [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 callarray(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: