Plotting the movement of vector

10 Ansichten (letzte 30 Tage)
N/A
N/A am 28 Okt. 2016
Kommentiert: Walter Roberson am 30 Okt. 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 Kommentare
James Tursa
James Tursa am 28 Okt. 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 am 28 Okt. 2016
that's a typo, please assume it's 2x1.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Walter Roberson
Walter Roberson am 29 Okt. 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 Kommentare
N/A
N/A am 29 Okt. 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 am 30 Okt. 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Graphics Objects finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by