rotate a matrix with a fixed grid position
5 views (last 30 days)
Show older comments
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
Answers (1)
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.
0 Comments
See Also
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!