登录论坛

查看完整版本 : Matlab矩阵和折叠列表


poster
2019-12-14, 20:13
我在mathematica中有两个问题,想在matlab中解决:

measure := RandomReal[] - 0.5 m = 10000; data = Table[measure, {m}]; fig1 = ListPlot[data, PlotStyle -> {PointSize[0.015]}] Histogram[data] Matlab的:

measure =@ (m) rand(1,m)-0.5 m=10000; for i=1:m data(:,i)=measure(:,i); end figure(1) plot(data,'b.','MarkerSize',0.015) figure(2) hist(data) 它给了我:

???从function_handle转换为double时发生以下错误:使用==> double时出错

如果我做 :

measure =rand()-0.5 m=10000; data=rand(1,m)-0.5 然后,我在plot1中得到正确的结果,但是在图2中,y =轴是错误的。

另外,如果我在mathematica中有这个:

steps[m_] := Table[2 RandomInteger[] - 1, {m}] steps[20] Walk1D[n_] := FoldList[Plus, 0, steps[n]] LastPoint1D[n_] := Fold[Plus, 0, steps[n]] ListPlot[Walk1D[10^4]] 我是这样做的:

steps = @ (m) 2*randint(1,m,2)-1; steps(20) Walk1D =@ (n) cumsum(0:steps(n)) --> this is ok i think LastPointold1D= @ (n) cumsum(0:steps(n)) LastPoint1D= @ (n) LastPointold1D(end)-->but here i now i must take the last "folding" Walk1D(10) LastPoint1D(10000) plot(Walk1D(10000),'b') 我得到一个空矩阵,没有图。



回答:

由于@Itamar本质上回答了您的第一个问题,因此以下是对第二个问题的评论。您做得差不多。您需要定义

Walk1D = @ (n) cumsum(steps(n)); 因为cumsum是FoldList[Plus,0,your-list]的直接类似物。然后,代码中的plot可以正常工作。另外,请注意,无论是在Mathematica还是Matlab代码中,都不必分别定义LastPoint1D在两种情况下,它都是生成的列表(向量) steps的最后一点。

编辑:

在LastPoint1D上扩展一点:我的猜测是,您希望它成为Walk1D计算的行走的最后一个点。因此,将IMO设为返回的最后一个点的生成的走行(矢量)函数将是有意义的。例如:

lastPoint1D = @(walk) (walk(end)); 然后,将其用作:

walk = Walk1D(10000); lastPoint1D(walk) 高温超导



更多&回答... (https://stackoverflow.com/questions/4990407)