I want to loop a value into a vector, how do I do this?

3 次查看(过去 30 天)
I am trying to loop through my script in order to get several values of one variable (D_N), and then putting all these values into an empty vector. So far I have made:
N = 10;
M = 5;
B = zeros(M,N); % empty vector to contain x_t.
F = zeros(M,1); % empty vector to contain d_N.
for m=1:M % This creates more random walks.
x_t(1) = 0;
for n = 1:N % end position of the random walk.
A = sign(randn);
x_t(n+1) = x_t(n) + A;
B(m,n) = x_t(n); % vector containing all of x_t.
end
plot(x_t);
hold on
C = B(:,end) % takes the last elements in the B vector.
D = C.^2 % ^2 on each of the elements in C.
D_N = sqrt(mean(D)) % This is what I want to loop several times.
end
F(m,1) = D_N % This is where I want to put all the values from the looped D_N.
For some reason putting a third for-loop makes my script behave oddly. Anyone know how to do this?
  1 个评论
Jan
Jan 2017-10-15
Please edit the question and add more details: Which 3rd loop do you mean? What is "odd"? What do you observe and expect instead?

请先登录,再进行评论。

回答(2 个)

Jan
Jan 2017-10-15
编辑:Jan 2017-10-15
Your D_N is overwritten in each iteration. Perhaps you want:
D_N(:, m) = sqrt(mean(D))
Are you sure with sign(randn)? There are cheaper methods to obtain a +/-1 and the very rare case of 0.
You can replace:
x_t(1) = 0;
for n = 1:N
A = sign(randn);
x_t(n+1) = x_t(n) + A;
B(m,n) = x_t(n); % vector containing all of x_t.
end
by
B(m, :) = [0, cumsum(1 - 2 * randi([0,1], 1, N - 1))];

dpb
dpb 2017-10-15
编辑:dpb 2017-10-15
You've got the assignment to F(m) outside the loop on m...
...
C = B(:,end) % takes the last elements in the B vector.
D = C.^2 % ^2 on each of the elements in C.
D_N = sqrt(mean(D)) % This is what I want to loop several times.
end
F(m,1) = D_N % This is where I want to put all the values from the looped D_N.
should be
...
C = B(:,end) % takes the last elements in the B vector.
D = C.^2 % ^2 on each of the elements in C.
D_N = sqrt(mean(D)) % This is what I want to loop several times.
F(m,1) = D_N % This is where I want to put all the values from the looped D_N.
end
which could be shortened to just
...
F(m)= rms(B(:,end));
end
without all the intermediaries you don't use and the builtin rms function. Also, don't need the second explicit subscript on F as you've already preallocated it as a column vector.
NB, however, you're doing all this for every m where B only has elements for those rows to which the loop has yet traversed...that's a lot of overhead for what are still all zeros until the loop finishes and recomputing the same thing for every row that has already been done. What you're really looking for is
x_t=zeros(1,N);
for m=1:M % This creates more random walks.
for n = 0:N-1 % your code built N+1 elements
x_t(n+1) = x_t(n) + sign(randn);
B(m,n) = x_t(n); % vector containing all of x_t.
end
plot(x_t);
hold on
end
F=rms(B(:,end)); % wait 'til end to compute the end vector of distance
which does the full array assignment to F -- with this syntax you don't need (or want) the variable m.
Also note you could remove the intermediary loop on m,n by building a random array of that size via
x=randn(M,N-1); % mxn-1 rand normals.
B=cumsum([zeros(M,1) sign(x)],2); % accumulate +/-1
F=rms(B(:,end));
  1 个评论
dpb
dpb 2017-10-15
ADDENDUM: Jan's comment on sign(randn) is very good one, btw, outside the code structure...

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Elementary Math 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by