从mathematica 到matlab 的转换 --> (附录)

发布于 2024-10-14 08:47:38 字数 1277 浏览 2 评论 0原文

我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

楠木可依 2024-10-21 08:47:38

为了将标量附加到 Matlab 数组,您可以调用 array(end+1) = valuearray = [array;value] (将分号替换为如果你想要一个 1×n 数组,则用逗号)。后者也适用于追加数组;要将数组附加到前者,您可以调用 array(end+1:end:size(newArray,1),:) = newArray 以防您想沿第一个维度进行串联。

然而,在 Matlab 中,追加执行超过 100 次迭代的循环是一个坏主意,因为它很慢。您最好首先预先分配数组 - 或者甚至更好,对计算进行矢量化,以便您根本不必循环。

如果我理解正确的话,您想从正态分布中越来越多的样本中计算平均值和 SEM。以下是如何使用循环执行此操作:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文