Move object in a direction described by a vector

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!

답변 (1개)

the cyclist
the cyclist 2013년 5월 4일

0 개 추천

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))

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

태그

질문:

2013년 5월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by