Make a point move around randomly as if it were a person walking

9 views (last 30 days)
I'm trying to make a point move towards the right but in random directions (always forwards however). I tried doing something with circshift:
r1x = [1,1]; %starting position
%random variables
n1 = round(20*rand(1))
n2 = round(20*rand(1))
hold on
axis([0 100 0 100]);
for T = 1:200
plot(T,r1x(T,T),'b*')
circshift(r1x,n1,n2)
pause(0.1)
end
This gave me the following error:
Index exceeds matrix dimensions.
Error in test (line 13)
plot(T,r1x(T,T),'b*')
Which I think basically means I can't plot the "updated" (new circshifted) position... So I tried using a for loop instead..
r1x = 1;
for i = 1:100
r1x(i) = 100*rand(1)+i
end
hold on
axis([0 100 0 100])
for t = 1:400
plot(t,r1x(t),'b*')
pause(0.1)
end
Which is more like what I want except I don't want to keep the old positions, and the new positions should look more like a person walking or running not just random dots. Any ideas/suggestions?
  2 Comments
Adam
Adam on 3 Nov 2016
Well, there's a basic syntax question in there and a far more complex question on modelling a person walking rather than just creating random dots. One is likely easy to fix, the other obviously is outside of the realm of just Matlab expertise and needs more domain knonwledge.
You get your first error because r1x never changes from the [1,1] that you assign to it. Your circshift instruction simply throws away its result so might as well not be there. You need to return a result as Matlab does not work on variables by reference (except for handle-derived objects)
Djdi djaia
Djdi djaia on 3 Nov 2016
Edited: Djdi djaia on 3 Nov 2016
Tried the following
for T = 1:200
plot(T,r1x(T,T),'b*')
r1x = circshift(r1x,round(20*rand(1)),round(20*rand(1)))
pause(0.1)
end
I changed n1 and n2 to be redefined within the for loop so that it can continuously find new random variables, and tried to update the r1x matrix but I get this error now:
r1x =
1 1
Index exceeds matrix dimensions.
Error in test (line 10)
plot(T,r1x(T,T),'b*')
And what I mean by a person walking is that you can identify some sort of path from the dots, not just random scatterings of points.

Sign in to comment.

Answers (1)

Nick Counts
Nick Counts on 3 Nov 2016
Your indexing errors stem from:
r1x(T,T)
You are asking for the Tth row and Tth column. That means after your second cycle, T is greater than 2, which means it is looking for a 3rd or greater column, and that doesn't exist!
It looks like you are trying to use T as a time stamp or the dependent variable for your plot. You seem to be changing an x,y pair in your model but you specify the 'x' for plotting with your T for loop.
As Adam said, there is a deeper modeling question here, but to get your code to output something without errors, try the following:
r1x = [1,1]; %starting position
%random variables
n1 = round(20*rand(1))
n2 = round(20*rand(1))
hold on
axis([0 100 0 100]);
for T = 1:200
plot(T,r1x(T,2),'b*')
% Assign the output of circshift to r1x so it can be used on the next
% loop
% You are building an array one line at a time. You need the index for
% the new row to go, and you need the index of the current row.
r1x(T+1, :) = circshift(r1x(T),n1,n2)
pause(0.1)
end
This should at least let you see what you were actually doing. I suspect circshift isn't doing what you expect.
Good luck!

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!