rotate a matrix with a fixed grid position

5 views (last 30 days)
Daniel Mella
Daniel Mella on 20 Jan 2018
Answered: Akanksha on 1 Mar 2025
Hi,
I have a matrix of values A with their corresponding coordinate contained within the matrices X and Y. I can visualise the data by simply typing pcolor(X,Y,A). Visually, the data seems rotated around its centre. So, I tried to rotate A using imrotate or affine2d but the resulting matrix B has different dimensions and I can no longer relate it to the coordinate system X,Y without changing it also. It is possible to rotate A without changing X,Y ? Maybe I have to interpolate?
Thank you very much, this is quite confusing for me.
  2 Comments
Image Analyst
Image Analyst on 20 Jan 2018
Attach your data and code, and a screenshot.
Daniel Mella
Daniel Mella on 20 Jan 2018
B=imrotate(A,1) produces a bigger matrix filled with zeros and I cannot relate it to [X,Y] With B=imrotate(A,1,'crop') I lose information on the corners introducing zero values.
Basically, I need to change the orientation of the matrix A without losing information and without losing its correspondence with the grid [X,Y]

Sign in to comment.

Answers (1)

Akanksha
Akanksha on 1 Mar 2025
The following code changes the orientation of matrix A by the desired angle (here 1 degrees) while being in correspondence with grid [X,Y] :
%% Grid of X and Y coordinates from -10 to 10 (200 points each)
[X, Y] = meshgrid(linspace(-10, 10, 200), linspace(-10, 10, 200));
% Matrix A
A = exp(- (X.^2 + Y.^2) / 50);
% Visualizing the original data
figure;
pcolor(X, Y, A);
shading interp;
colorbar;
title('Original Data');
%%Defining the Rotation Transformation
theta = 1; % rotation angle in degrees
tform = affine2d([cosd(theta) -sind(theta) 0;
sind(theta) cosd(theta) 0;
0 0 1]);
%% Creating the Spatial Referencing Object
% The imref2d object defines the world coordinate limits for A.
Rin = imref2d(size(A), [min(X(:)), max(X(:))], [min(Y(:)), max(Y(:))]);
%% Applying the Transformation with imwarp
% Use the 'OutputView' parameter to force the output to have the same coordinate grid as Rin.
B = imwarp(A, tform, 'OutputView', Rin);
%% Visualizing the Rotated Data
figure;
pcolor(X, Y, B);
shading interp;
colorbar;
title('Rotated Data');
I have run the above code locally, and successfully produced the required output. I will attach the original and transformed data visualizations for your reference below -
Also find the links to required documentation for further reference :
Hope this helps. Thanks.

Categories

Find more on Interpolation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!