Shift vector function in MATLAB

I am working on my own shift vector function that will be used later to compute the convolution of two signals. The function has to shift the vectors either left or right depending on the magnitude and direction of the shift value. My code is:

%This function shifts a vector either left or right

function sv = shiftvector(v, n) %Where v is the vector and
%n is the shift value
vnew = zeros(1,length(v));

for i = 1:length(v)-n

if n>0

vnew(i) = v(i+n);

elseif n<0

vnew(i+1:end) = v(1:end-n);

end

end
sv = vnew;
end


The issue I have is, whenever I input a negative shift value, MATLAB gives an error. Need help!

• How about using $circshift()$ ? – Hilmar Oct 12 '19 at 16:37

You must understand an important indexing issue when simulating algorithms using computers. In particular linear shifting, as negative indices do not exist. You cannot arbitrarily shift a vector to left or right without before adjusting the frame/window/block size.

Consider the following signal :

$$x[n] = \{7,9,0,4,8\}$$

It has five nonzero samples for the time indices $$n = \{0,1,2,3,4\}$$.

Its Matlab representation can be with the following vector / matrix:

xv = [7,9,0,4,8]


Here the time-index of the first vector-sample "7" is an implied zero. (and it's actually one wrt the array/matrix indexing). In other words $$x_v(1) = x[0]$$

When you shift this sequence to left, by one sample to obtain $$y[n] = x[n+1]$$, then its beginning sample "7" will now have an index of $$n=-1$$, which you cannot have in Matlab (or any other computer language). What you can do is to simulate this in a roundabout way; i.e., create a large enough observation frame length say $$n \in \{-L,L \}$$ for some integer $$L$$, then assign the sequence time-index $$0$$ to the matlab-vector index $$i=L+1$$. This will work as long as your sequence lengths + shifts are shorter than window length.

Similar problem occurs for right shifts which can be avoided by enlarging the output vector that holds the shifted seqeunce.

The following is an example implementation of the idea :

% simulates LINEAR shift of vectors using a frame length
clc; clear all; close all;

L = 32;         % symmetric time frame [-L,L]
n = [-L:L];     % time index for plotting
N = length(n);  % length of the frame

ind0 = L+1;     % time-index 0 is at matlab index ind0

x = zeros(1,N); % row-vector to hold the sequence x[n]
x(ind0:ind0+4) = [7,9,-3,2,5];

% SHIFT FUNCTION:
d = 7;          % y[n] = x[n-d];
y = zeros(1,N);

if (d > L+1) | (d < -L-length(x))
error('shift out of window range...');
elseif d >= 0
y(d+1:end) = x(1:end-d);
else % d < 0
y(1:end+d) = x(-d+1:end);
end

% DISPLAY:
figure,stem(n,x);
title('x[n]');

figure,stem(n,y);
title(['y[n] = x[n-d] for d = ',num2str(d)]);


Matlab does not accept lesser than $$1$$ indices. Other languages often accept a $$0$$-based index. More about Shifting Sequences Using MATLAB is available, including function circshift.m.

• Ok, I see. Oh by the way, is shifting a vector similar to performing adding/subtracting the shift value on the original vector? – AugieJavax98 Oct 12 '19 at 16:20
• How do you define ' adding/subtracting the shift value' adding/subtracting the shift value – Laurent Duval Oct 12 '19 at 16:23