【问题标题】:Translating the map command and few others from Mathematica to MATLAB将 map 命令和其他一些命令从 Mathematica 翻译到 MATLAB
【发布时间】:2011-02-17 17:00:15
【问题描述】:

到目前为止我做了这些:

编辑---------------

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

nsteps=200;
nsq=floor(sqrt(nsteps));
MeanSquareDistance1D= @ (n,m) m.*sum((LastPoint1D(n)).^2)./m;
r2D=MeanSquareDistance1D(100,1000)

data=[ ];
for i=10:20:90
data=[data; i , MeanSquareDistance1D(i,2000)]
end

现在唯一的问题是,“数据”的第二列必须给我周围的值

10
30
50
70
90

但不完全。只是近似。例如,“数据”必须是:

10  10.184
30  27.51
50  50.306
70  68.394
90  90.414

总和可能有问题?

【问题讨论】:

  • 上一个问题可能对您有所帮助:Map function in MATLAB?
  • 谢谢,我看到了这个问题,但我的问题是我无法按原样表达“数据”。

标签: matlab map wolfram-mathematica


【解决方案1】:

虽然 Matlab 支持匿名函数并且对函数式编程有基本的支持,但它的支持并不特别好,也不是惯用的。 Matlab 方法是尽可能使用向量(数组的 Matlab 名称),并使用对这些向量进行评估的函数。

代替

Walk1D =@ (n) cumsum(steps(n));

这不起作用,因为cumsum(需要一个向量)的参数只是steps(n)(向量steps的单个元素!),惯用的Matlab方法是这样做:

Nsteps = 100;  
steps = randn(1, Nsteps);
walk1D = cumsum(steps);

等等

如果您确实需要它,函数式编程“映射”运算符的 Matlab 等效项是 cellfunarrayfun

(旁白:与受 Lisp 启发的 Mathematica 相比,Matlab 的传统植根于 Fortran。Matlab 中的匿名函数仅限于简单的表达式,因此通常需要使用存储在单独的 .m 中的传统命名函数文件。我经常觉得 Matlab 中的函数式编程结构大多是新奇的,尽管它们有时很有用。)

【讨论】:

  • 您好,Walk1D 功能工作正常,但感谢您给我的方法。我在写上面的“数据”时遇到问题,因为我有点困惑该写什么!
【解决方案2】:

我会稍微评论一下您的 Mathematica 输入,建议提高效率

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

这里是显示差异的时间

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}

【讨论】:

    【解决方案3】:

    从您的代码中,我猜您想计算一维随机游走的均方距离。

    滞后tt 处的均方距离是沿由tt 步长分隔的随机游走的两个位置之间的均方差。我假设data 应该是一个数组,其中第一列是tt,第二列是相应的均方距离,并且还有一个附加参数表示随机游走的总步数。

    这是我计算data的方法

    %# define parameters
    nSteps = 2000;
    listOfLags = 10:20:90; %# must be 1-by-n vector
    
    %# create random walk
    %# steps can be +1 or -1, add all of them via cumsum
    randomWalk = cumsum(randi([0 2],nSteps)-1);
    
    %# calculate msd for the desired lags
    %# use a loop for readability
    nLags = length(listOfLags);
    data = zeros(nLags,2);
    data(:,1) = listOfLags;
    
    for lag = listOfLags
        %# lag takes on every lag value, so use logical indexing to find
        %# which lag (in terms of entry into data) we're currently working on
    
        %# This line corresponds to
        %# 1. get all distances traveled within a duration of `lag`
        %#    vectorOfDistances = randomWalk(lag+1:end) - randomWalk(1:nSteps-lag)
        %#    i.e. the first element is randomWalk(lag+1)-randomWalk(1)
        %# 2. square all: (vectorOfDistances).^2
        %# 3. average all squared distances 
        data(listOfLags==lag,2) = mean( (randomWalk(lag+1:end) - randomWalk(1:end-lag)).^2);
    end
    
    %# plot the results
    plot(data(:,1),data(:,2),'.')
    xlabel('lag'),ylabel('mean squared displacement')
    

    【讨论】:

    • 首先再次感谢!我很难遵循您的代码,因为我用另一种方式(Walk1D,LastPoint1D 等)措辞。我将在一分钟内编辑我的原始帖子,向您展示哪里我得到了。(我试过你的代码,但给了我“??未定义的函数或变量'滞后'。)
    • 如果你帮不上忙,没关系,乔纳斯,你帮我的已经够多了..你没有时间陪我 :)
    • @George:很抱歉这个错误。如果你做我认为你想做的事,你很可能做错了,因为对于均方距离,你必须在距离向量上取平均值,你的 LastPoint1D 返回一个标量,@987654328 @ 除了浪费循环之外,实际上并没有做任何事情。
    • 我必须像“(sum((LastPoint(n)-LastPoint(0)).^2))./m”那样做某事,但怎么做?
    • 我是这样做的(在帮助下):steps1=@ (n, m) randi([-1 1], n, m); LastPoint_1D=@(n, m) sum(steps1(n, m)); MeanSquareDistance1D = @(n,m) mean(LastPoint_1D(n,m).^2);
    猜你喜欢
    • 1970-01-01
    • 2020-09-14
    • 2018-04-09
    • 2017-12-20
    • 1970-01-01
    • 1970-01-01
    • 2013-05-30
    • 2020-03-09
    • 1970-01-01
    相关资源
    最近更新 更多