Move object in a direction described by a vector
Afficher commentaires plus anciens
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!
Réponses (1)
the cyclist
le 4 Mai 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))
Catégories
En savoir plus sur Programming dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!