forked from JesseLu/objective-first
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ob1_shift_matrix.m
61 lines (47 loc) · 1.49 KB
/
ob1_shift_matrix.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
function [A] = ob1_shift_matrix(dims, shift)
% A = OB1_SHIFT_MATRIX(DIMS, SHIFT)
%
% Description
% Create a matrix that will shift the values of a vector, where the vector
% represents a 2D grid of values. Assumes periodic/circular boundary
% conditions.
%
% Inputs
% DIMS: 2-element vector of positive integers.
% DIMS = [XX YY], where XX and YY are the number of grid points in the
% horizontal and vertical directions, respectively, of a grid of values
% in vector form.
%
% SHIFT: 2-element vector of integers.
% SHIFT consists of [SX SY], where SX is the horizontal shift and SY
% is the vertical shift. Positive values shift downwards and to the
% right, while negative values shift upwards and to the left.
%
% Outputs
% A: Sparse matrix.
% Sparse matrix that will perform the shift.
N = prod(dims); % Final matrix will be NxN.
%
% Matrix with place-holder values.
%
A = reshape(1:N, dims);
%
% Shift in the horizontal (x) direction.
%
if (shift(1) > 0)
A = [A(end-shift(1)+1:end,:); A(1:end-shift(1),:)];
elseif (shift(1) < 0)
A = [A(-shift(1)+1:end,:); A(1:-shift(1),:)];
end
%
% Shift in the vertical (y) direction.
%
if (shift(2) > 0)
A = [A(:,end-shift(2)+1:end), A(:,1:end-shift(2))];
elseif (shift(2) < 0)
A = [A(:,-shift(2)+1:end), A(:,1:-shift(2))];
end
%
% Now form the actual, sparse shifting matrix.
%
A = sparse(1:N, A(:), ones(1, N), N, N);