将地图命令和其他几个从Mathematica翻译成MATLAB [英] Translating the map command and few others from Mathematica to MATLAB

查看:133
本文介绍了将地图命令和其他几个从Mathematica翻译成MATLAB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我到目前为止这样做:

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

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

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

现在唯一的问题是数据的第二栏必须给我围绕

The only problem now,is that the 2nd column of "data" must give me values around

10
30
50
70
90

但不完全只有大约。例如,数据必须是:

but not exactly.Only approximately.For example ,the "data" must be:

10  10.184
30  27.51
50  50.306
70  68.394
90  90.414

Sth可能是错误的?

Sth is wrong with the sum maybe?

推荐答案

从你的代码我想你想要计算1D随机游走的均方距离。

From your code I guess you want to calculate the mean squared distance for a 1D random walk.

滞后时间的平均距离 tt 是随机游走两个位置之间的平均差距, code> tt 步骤。我假设数据应该是第一列是 tt 的数组,第二列是对应的均方距离,那里有一个额外的参数,指示你随机游走的总步数。

The mean squared distance at lag tt is the average squared difference between two positions along the random walk separated by tt steps. I assume that data should be an array where the first column is tt and the second column the corresponding mean squared distance, and where there is an additional parameter that indicates the total number of steps in your random walk.

这是我如何计算数据

%# 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')

这篇关于将地图命令和其他几个从Mathematica翻译成MATLAB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆