# Matrix Manipulation/Shifting

14 views (last 30 days)
Aldo Hernandez on 15 Oct 2019
Answered: Guru Mohanty on 16 Jan 2020 at 7:07
I am trying to shift a 12x3 matrix using imwarp function. I know there are other ways to do this, but for future work it must be done with imwarp function though I am runnning into problems. The code:
%Shifting of a matrix
%Definng a matrix:
Matrix=[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];
%Defining moving points and fixed points
movingp=[1,1;1,2;1,3;2,1;2,2;2,3;3,1;3,2;3,3;10,1;10,2;10,3;11,1;11,2;11,3;12,1;12,2;12,3];
fixedp=[4,1;4,2;4,3;5,1;5,2;5,3;6,1;6,2;6,3;7,1;7,2;7,3;8,1;8,2;8,3;9,1;9,2;9,3];
%Defining twarp for imwarp and running function
I am trying to shift the top three rows and the bottom three rows of the matrix to the right by one unit with respect to the center 6 rows:
For example for the top six rows, it should look something like this:
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
I am assuming I need to better define the moving points to include a function which does the movement. I know there are other, better ways to do the matrix shifting, though this function is necessary for future work of mine so I am mostly trying to get a better understanding of how to use it. I just need help finishing the code which will shift the top 3 and bottom 3 rows to the right with respect to the center 6 rows. The purpose if for image alterations, meaning the fitgeotrans and imwarp functions should not change and are crucial. I'm assuming the function will warp the 'image' to correct for the matrix expanding and shifting of matrix values.

Guru Mohanty on 16 Jan 2020 at 7:07
Hi, I understand you are trying to shift matrix using imwarp function. You can do this by manipulating Displacement field of the imwarp function. Here is a sample code.
clc;clear all;
%Definng a matrix:
Matrix=[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];
[r,c]=size(Matrix);
%Out_mat=zeros(r,c+1);
%Defining Displacement
X_disp=[-ones(3,c+1);zeros(6,c+1);-ones(3,c+1)]; % X displacement
Y_disp=zeros(r,c+1); % Y displacement
Disp(:,:,1)=X_disp;
Disp(:,:,2)=Y_disp;
%Imwrap
Out_mat=imwarp([Matrix zeros(r,1)],Disp);