Translating the map command and few others from Mathematica to MATLAB

后端 未结 3 1630
我寻月下人不归
我寻月下人不归 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:35

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

    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.

    Here's how I'd calculate 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')
    

提交回复
热议问题