![16Dec2.PNG](https://www.mathworks.com/matlabcentral/answers/uploaded_files/262026/image.png)
Matrix Manipulation/Shifting
3 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Guru Mohanty
2020-1-16
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](https://www.mathworks.com/matlabcentral/answers/uploaded_files/262026/image.png)
2 个评论
Guru Mohanty
2020-2-13
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/271295/image.png)
There is no shift in Y Co-ordinate So the Y Co-ordinate Shift would be zero
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/271296/image.png)
The matrix Disp is Combination of both X Co-ordinate Shift & Y Co-ordinate Shift. After getting Displacement field you can use imwrap function.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!