Matrix Manipulation/Shifting

4 views (last 30 days)
Aldo Hernandez
Aldo Hernandez on 15 Oct 2019
Commented: Guru Mohanty on 13 Feb 2020
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
pleasework=fitgeotrans(movingp,fixedp,'polynomial',2);
A=imwarp(Matrix,pleasework)
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.

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];
[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);
16Dec2.PNG
  2 Comments
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)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!