Averaging a Random Walk with Numbers Spanning [-1,1]

1 回表示 (過去 30 日間)
Michael
Michael 2012 年 11 月 8 日
コメント済み: Michael 2013 年 12 月 11 日
Hello,
I need a random number generator whose values span [-1,1] (was using 2*rand-1) and I'm going to count the number of sign changes, then run that a whooole bunch of times and then average the sign changes.
I feel like I'm getting good results when I don't incorporate the '-1' part of the 2*rand-1... can anyone help me clear this issue up?
% Begin Loop for averaging sign changes
for j=1:10 % Number of iterations: better statistics the larger this is
clear count T starty e i
% Preallocate and such
steps=10; % Number of steps to be taken! should be large.. was testing
% small values
y_init=0;
y=y_init*ones(1,steps);
e=2*randn(1,steps)-1; % Numbers span [-1,1]
count=0;
%
% Begin loop to (repetitively) count sign changes in random walk
for i=1:(steps-1);
y(i+1)=y(i)+e(i+1)
end
%
% From MATLAB 'help rand'
% Example 1: Generate values from the uniform distribution on the
% interval [a, b].
% r = a + (b-a).*rand(100,1);
% newstep= -1 + 2.*rand(100,1)
%
zeros(j)=length(find(abs(diff(sign(y)))==2));
if j==1
time=[1:1:steps];
plotrandom=plot(time,y);
end
end
AVGnoBounces=mean(zeros)
Thanks for any help!!! -Mike
  2 件のコメント
Walter Roberson
Walter Roberson 2012 年 11 月 8 日
Naming a variable "zeros" is going to interfere with use of the zeros() function, and is going to confuse readers.
Michael
Michael 2013 年 4 月 3 日
Sorry...

サインインしてコメントする。

採用された回答

Matt Fig
Matt Fig 2012 年 11 月 9 日
編集済み: Matt Fig 2012 年 11 月 9 日
Wouldn't this be easier?
T = 2*rand(1,10)-1;
Num_change = sum(logical(diff(sign(T))))
  1 件のコメント
Michael
Michael 2013 年 12 月 11 日
So Sorry I haven't seen your result or thanked you!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by