MATLAB Answers

Matrix Manipulation/Shifting

5 views (last 30 days)
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
%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.


Sign in to comment.

Accepted Answer

Guru Mohanty
Guru Mohanty on 16 Jan 2020
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];
%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
Out_mat=imwarp([Matrix zeros(r,1)],Disp);


Aldo Hernandez
Aldo Hernandez on 12 Feb 2020
Thank you for your answer! It's been super helpful.
I was hoping you could explain what is happening a bit clearer in the %Defining Displacement section of the code. This is exactly what I am looking for but I am still unsure how to translate it to larger matrices.
Guru Mohanty
Guru Mohanty on 13 Feb 2020
The Output matrix is of 12x4 size, So Zero padding is required. The Displacement field of the imwrap function has 2 components-
1. X Co-ordinate Shift.
2. Y Co-ordinate Shift.
In this Scenario the first three and last three rows are X shifted. So X Co-ordinate Shift would be like this
There is no shift in Y Co-ordinate So the Y Co-ordinate Shift would be zero
The matrix Disp is Combination of both X Co-ordinate Shift & Y Co-ordinate Shift. After getting Displacement field you can use imwrap function.

Sign in to comment.

More Answers (0)