Plotting the movement of vector

8 vues (au cours des 30 derniers jours)
N/A
N/A le 28 Oct 2016
Commenté : Walter Roberson le 30 Oct 2016
I have implemented a function where I take M matrix and apply it to a random 2d vector x. As for loop increments I would like to plot the movement of the vector x. So x, Mx, m^2x... as positions in 2d space. I have already written the function and would like suggestions/pointers to complete it.
function vector_pos(M)
for k = 1:10
v = randi(10, 2, 1); %generate random 2d vector%
x = v * M; %applies matrix M to 2D vector%
figure();
plotv(x,'o'); %suppose to plot the position of the vector%
pause;
end
end
Thank you.
  2 commentaires
James Tursa
James Tursa le 28 Oct 2016
I don't understand why v starts as a 2x2 matrix. Why isn't it a 2x1 vector? Your code doesn't seem to match your description.
N/A
N/A le 28 Oct 2016
that's a typo, please assume it's 2x1.

Connectez-vous pour commenter.

Réponses (1)

Walter Roberson
Walter Roberson le 29 Oct 2016
function vector_pos(M)
assert( isequal(size(M), [2 2]);
v = randi(10, 2, 1); %generate random 2d vector%
x = ones(2, 1);
for k = 1:10
x = M * x; %applies matrix M to 2D vector%
figure();
plotv(x,'o'); %suppose to plot the position of the vector%
pause;
end
end
Notice that I switched to left multiplying from right multiplying.
With your input vector being 2 x 1, when you right multiply, in order to get out a 2 x 1 vector, M would have to be a scalar to satisfy the dimensions: (2 x 1) * (1 x N) -> 2 x N and since output must be 2 x 1, N = 1, so the M would have to be 1 x 1.
With left multiplying, then you get (N x 2) * (2 x 1) -> N x 1 and since that must be 2 x 1, that gives M as 2 x 2 .
If you want to right multiply then you need to switch your v and x to be 1 x 2 instead of 2 x 1. But note your own notation in the question, where you show m^2x. which is left multiplication not right multiplication.
  2 commentaires
N/A
N/A le 29 Oct 2016
Thank you. Does that code plot the position of vector? Because I do get a plot but I'm not sure if that's what I was trying to achieve.
Walter Roberson
Walter Roberson le 30 Oct 2016
The code calls
plotv(x,'o'); %suppose to plot the position of the vector%
which does whatever it does. Are you using https://www.mathworks.com/help/nnet/ref/plotv.html ? That routine expects 2 by something, and 2 x 1 like you use is valid for it, but it would plot only one vector in such a case.
There would not seem to be a lot of benefit to having both figure() and pause() in your loop: the figure() would result in creating a new figure and if each output is in a new figure there is no need to pause between them. You should consider removing the figure() call and replacing pause with drawnow() . Or perhaps you should store all of the values and call plotv() only once at the end... it is not clear to us what your expected results are.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Graphics Objects dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by