【发布时间】:2011-02-20 19:18:34
【问题描述】:
我想问这个: 我有数学:
step := {{0, 1}, {1, 0}, {0, -1}, {-1, 0}} [[RandomInteger[ {1, 4}] ]]
step --> this takes one list from above (for example {0,1})
steps2D[n_] := Table[step, {n}]
我做到了:
a=[0,1];b=[1,0];c=[0,-1];d=[-1,0];
list=[a;b;c;d]
step=@ (rand) rand(1,list) -->> i must extract from here randomly one pair..
step
steps2D=@ (n) arrayfun(step,n);
我有两个问题: 1)我不能从我的列表中随机抽取一对。 2) 我不知道我的 step2D 是否正确。
EDIT-->> 代码继续:
Walk2D[n_] := FoldList[Plus, {0, 0}, steps2D[n]]
Walk2D[10]
LastPoint2D[n_] := Fold[Plus, {0, 0}, steps2D[n]]
LastPoint2D[100]
我这样做了:
Walk2D=@ (list,n) cumsum(steps2D(list,n));
Walk2D(list,10)
LastPoint2D = @ (Walk2D) (Walk2D(end));
walk1=Walk2D(list,100);
LastPoint2D(walk1) -->> This gives me only one number and not a pair as it should
【问题讨论】:
-
你总是很难将 Mathematica 翻译成 Matlab,因为它们有不同的哲学。 Mathematica 鼓励函数式编程; Matlab 改为对数组进行操作。