from
Shift Vector
by Sebastian
Shifts a row or column vector by an input number of elements
|
| shiftVector(A,shiftsize,shiftMode)
|
function B = shiftVector(A,shiftsize,shiftMode)
%SHIFTVECTOR shifts the input column or row vector by shiftsize
% B = shiftVector(A,shiftsize) will shift the vector A left/up (if
% shiftsize is positive) or right/down (if shiftsize is negative) by
% shiftsize steps, and move the elements that are cut off back to the
% opposite end.
%
% Example 1:
%
% A = 1:8;
% B = shiftVector(A, 3)
%
% B =
% 4 5 6 7 8 1 2 3
%
%
% Example 2:
%
% A = (1:8)';
% B = shiftVector(A, -2)
%
% B =
% 7
% 8
% 1
% 2
% 3
% 4
% 5
% 6
% Check that input is a row or column vector
if size(A,1) ~= 1 && size(A,2) ~= 1
error('Input A must be a row or column vector');
end
% Only rotate less than length of A in case shiftsize >= length(A)
% Also ensure directionality of rotation
lengthA = length(A);
shiftsize = mod(shiftsize, lengthA);
% Repeat vector twice
row = size(A,1) < size(A,2);
repmatTimes = [~row+1, row+1];
A = repmat(A, repmatTimes);
% Select inner elements
B = A(shiftsize + 1 : shiftsize + lengthA);
end
|
|
Contact us