1

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
Shai
  • 111,146
  • 38
  • 238
  • 371
George
  • 5,808
  • 15
  • 83
  • 160

1 Answers1

2

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
Jonas
  • 74,690
  • 10
  • 137
  • 177
  • Thanks for your answer Jonas!You were very helpful.I can't understand one part though.How we apply directly f(x) on the array xpoints.I can't understand why we don't use sth like :fx(xpoints(k)). – George Jan 25 '11 at 16:45
  • @George: xpoints is an array. Mathematical operations are applied element-wise on an array (note that if an operation has a special meaning lin linear algebra, such as multiplication, you need to use `.*` instead of `*` to get element-wise behavior). `exp(-x.^2)` works on each element of the array, so you can just pass an array for `x`, and don't need a loop. – Jonas Jan 25 '11 at 16:51
  • yes,but in order for exp(-x.^2) to work on each element of the array xpoints,don't we have somehow to put the exp(-x.^2) on the array?Instead,we are just manipulating xpoints.Where is the fx to take action on the xpoints?Sorry if i can't be understood.. – George Jan 25 '11 at 17:00
  • @George: You can either write `exp(-xpoints.^2)`, or you can write `f=@(x)exp(-x.^2);` and then call `f(xpoints)`. – Jonas Jan 25 '11 at 17:33
  • Thats right.In the code you gave me there is nowhere this.It's missing a line? – George Jan 25 '11 at 17:38
  • @George: In the Mathematica code, you create a uniform random variable and then transform it with f(x). In my code, I generate normally distributed variables directly. To reproduce the Mathematica code, create `xpoints` by calling `rand`, and then apply `f` to it. – Jonas Jan 25 '11 at 17:44
  • Alright ,i did this and i think its ok : a1t=fx(xpoints(1:np1)) , m = mean(a1t); sem = std(a1t)/sqrt(length(a1t)) – George Jan 25 '11 at 17:55
  • @George: Yes, that looks fine. Though you don't need to specify `1:np1` when passing `xpoints`, since `xpoints` has `np1` elements, anyway. – Jonas Jan 25 '11 at 17:56
  • Hello, how can i plot " sigmat" (simple plot)?And also "intmc" with errorbars?Because i can't figure a way to handle plot.I have problems with the dimensions. – George Jan 26 '11 at 12:56
  • @George: It's easiest to just call `plot(sigmat)`. And for errobars, you do `plot(intmc),hold on,errorbar(intmc,sigmat)` – Jonas Jan 26 '11 at 13:58
  • If i do "plot(sigmat)"it gives me wrong results because it gives as x axis the ntr=20points and as y axis the np1 points.I want as x axis the ntr points and as y the value of sigmat.---- As for errorbar(intmc,sigmat) it gives me error ->X, Y and error bars must all be the same length – George Jan 26 '11 at 14:05
  • @George: Ah, yes. I forgot they weren't vectors. `plot(sigmat(:,1),sigmat(:,2))` and `plot(intmc(:,1),intmc(:,2)),hold on, errobar(intmc(:,1),intmc(:,2),intmc(:,3))` – Jonas Jan 26 '11 at 14:11
  • The first worked great!But at the plot woth the errorbar ,it gives me the right plot(i used 'r.',to plot points) but without the error bars.And it gives me a message "Undefined function or method 'errobar' for input arguments of type 'double'." – George Jan 26 '11 at 14:20
  • @George: Yes, it's a typo. It should say `errorbar`. I strongly suggest you search the documentation for `plot` and `errorbar` to find out more about the commands. – Jonas Jan 26 '11 at 15:20
  • It seems to be ok though...Anyway ,i will search it more.You have helped me a lot Jonas and i thank you very much! – George Jan 26 '11 at 15:40
  • Ok,it was typing ! I wrote errobar instead of errorbar.Now,it works fine!Thanks again Jonas! – George Jan 26 '11 at 15:47
  • @Jonas: Jonas,if you could help me with this http://stackoverflow.com/questions/5032029/map-command-and-few-other-from-mathematica-to-matlab because i couldn't found a solution yet... – George Feb 22 '11 at 09:50