Repeating while loop to form matrix

I am simulating a random walk, I have a while loop that creates an array of the distance the particle is from the starting point for each iteration. I would like to then take this while loop and repeat it to simulate multiple particles at once, and then create a matrix that combined all the previous arrays. I am trying to repeat the while loop with a for loop. RIght now it only gives me back the matrix but with one row (when in this example it should have 3) and when I get multiple rows its all of the same array. ANy help would be appreciated
i = 10; %iteration time step
t = 0; %current time
p = 0; % current position
d = 0; %distance from orgin
A = zeros(1,i);%array of distances
B = [1:i];
p = 3 %number of particles
multiple_distance=[] %matrix of distances
for ii = 3
while t < i
r=rand;
if r<0.5
l=1;
else
l=-1;
end
x = x + l;
d = abs(0-x);
t = t+1;
A(t)=x;
end
A
multiple_distance = [multiple_distance ; A]
end

답변 (1개)

Bob Thompson
Bob Thompson 2021년 6월 4일
편집: Bob Thompson 2021년 6월 4일

0 개 추천

You've only indexed one value (ii = 3 means it will only ever be 3), which is why you're only getting one line completed.
for ii = 1:3 % Loops through all values from 1 to 3
while t < i
r=rand;
if r<0.5
l=1;
else
l=-1;
end
x = x + l;
d = abs(0-x);
t = t+1;
A(t)=x;
end
A
multiple_distance = [multiple_distance ; A]
end
I'm pretty sure this can be done without loops at all, but I'm going to have to spend a bit more time figuring out what the code is doing before I can give you an answer on that.

댓글 수: 4

Jo
Jo 2021년 6월 4일
Hello, thanks that gets me a 3 row matrix but its still all the same values each line, when they should be different. Do I need to assign the array to something different before I add it to the matrix?
Bob Thompson
Bob Thompson 2021년 6월 4일
편집: Bob Thompson 2021년 6월 4일
Oh, it's giving the same results because t is not being reset for each part of the for loop. So it runs the while loop the first iteration, and then finds that the conditions are closed (t~<i) so it skips the while loop for the second and third loops of the for, and just outputs the same A.
In any event, I was able to modify things to work without loops, so it should be faster to work on the entire matrix at once. This is assuming I actually understood what the loops are trying to accomplish.
i = 10; % Number of steps to take per particle
p = 3; % Number of particles
A = rand(p,i); % Get random numbers for all steps for all particles
A(A>=0.5) = -1; % Your previous l check
A(A<0.5&>0) = 1; % Else condition
multiple_distance = cumsum(A,2); % Sum steps across each row to get cumulative distance
Jo
Jo 2021년 6월 4일
thank you so much when i reset all the variables after the while loop it works, and for your code is there a way to do it so its not the cumulative but just the regular values in the matrix?
Bob Thompson
Bob Thompson 2021년 6월 4일
I'm not sure I understand what you mean by 'regular' vs 'cumulative.' I gave cumulative values because you were previously tracking x, which was the cumulative of each step. If you just want to get the +/- for each step the A already has those.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

Jo
2021년 6월 4일

댓글:

2021년 6월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by