Move object in a direction described by a vector
Show older comments
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!
Answers (1)
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))
Categories
Find more on Creating and Concatenating Matrices 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!