Translating the map command and few others from Mathematica to MATLAB

后端 未结 3 1608
我寻月下人不归
我寻月下人不归 2020-12-11 13:18

I did these so far:

EDIT---------------

steps=@ (m) 2*randi([0,1],[1,m])-1;
Walk1D =@ (n) [0,cumsum(steps(n))];
findend=@ (x) x(end);
LastPoint1D=@(n         


        
3条回答
  •  执笔经年
    2020-12-11 13:39

    I would comment on your Mathematica input a little, suggesting efficiency improvements

    Walk1D[n_] :=  Join[{0},Accumulate[steps[n]]]
    LastPoint1D[n_] := Total[steps[n]]
    

    Here are timings showing the difference

    In[51]:= steps[n_Integer] := RandomInteger[{-10, 10}, n]
    
    In[52]:= Walk1D[n_] := Join[{0}, Accumulate[steps[n]]]
    
    In[53]:= Walk1Da[n_] := FoldList[Plus, 0, steps[n]]
    
    In[56]:= BlockRandom[SeedRandom[1]; AbsoluteTiming[r = Walk1D[10^7];]]
    
    Out[56]= {0.3650000, Null}
    
    In[57]:= BlockRandom[SeedRandom[1]; AbsoluteTiming[r = Walk1Da[10^7];]]
    
    Out[57]= {1.1370000, Null}
    

提交回复
热议问题