Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Move object in a direction described by a vector

Asked by Tiger on 4 May 2013

Example

A=[0 1 2 3; 4 5 6 7; 8 9 10 11]

vector=[-1,-1]

I wish I could get the answer B=[ 0 0 0 0; 1 2 3 0; 5 6 7 0]

Thank you!

0 Comments

Tiger

Tags

Products

No products are associated with this question.

1 Answer

Answer by the cyclist on 4 May 2013

This is awkward, but I think it works:

% Your inputs
A = [0 1  2  3;
     4 5  6  7;
     8 9 10 11]
vector = [-1,-1]
% The algorithm:
vector(2) = -vector(2); % Because you defined the y-direction opposite to the MATLAB convention
[m,n] = size(A);
frame = zeros([m n]+abs(vector));
offset = max([0 0],-vector);
frame(offset(1)+(1:m),offset(2)+(1:n)) = A;
grab = max([0 0],vector);
B = frame(grab(1)+(1:m),grab(2)+(1:n))

0 Comments

the cyclist

Contact us