Make a point move around randomly as if it were a person walking
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Adam
el 3 de Nov. de 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)
Respuestas (1)
Nick Counts
el 3 de Nov. de 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!
0 comentarios
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!