1

私は数学で次のことをしていて、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
4

1 に答える 1

2

Matlab配列にスカラーを追加するには、または(1行n列の配列が必要な場合はセミコロンをコンマに置き換えます)のいずれarray(end+1) = valueかを呼び出すことができます。array = [array;value]後者は、配列を追加する場合にも機能します。前者で配列を追加するarray(end+1:end:size(newArray,1),:) = newArrayには、最初の次元に沿って連結する場合に備えて呼び出します。

ただし、たとえば100回を超える反復を行うループを追加することは、Matlabでは遅いため、お勧めできません。最初に配列を事前に割り当てるか、ループする必要がまったくないように計算をベクトル化することをお勧めします。

私があなたを正しく理解しているなら、あなたは正規分布からの増え続けるサンプルから平均と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
于 2011-01-25T16:18:10.460 に答える