MATLAB: How to track indices of a matrix after a transformation

indexingtransformation

I have a mxn matrix A that is transformed in some way to formulate B. The transformation can either be simple rotation or some other combination of transformations like flipud(rot(A,90)) etc. I am looking for some way to track the indices of A after any such transformation.
For Example
A = [1 2 3; 4 5 6; 7 8 9]
B = rot(A,90) = [3 6 9;2 5 8;1 4 7]
Original_Index = [1 1;1 2;1 3;2 1;2 2;2 3;3 1;3 2;3 2] (Orginal_Index is a 9×2 matrix that contains the row col indices of entries of A in Row Raster manner)
Transformed_Index = [3 1;2 1;1 1;3 2;2 2;1 2;3 3;2 3;1 3] (Also a 9×2 matrix which stores the row col indices of the entries after transformation)
I would appreciate any help in this regard.

Best Answer

Just make a matrix of the linear indexes and do the same thing to it that you do to your main matrix and you'll always know where the original element went to.
m = magic(6) % Sample data.
% Get rows and columns.
[rows, columns] = size(m)
% Construct a map of where the elements started out.
linearIndexes = reshape(1:numel(m), [rows columns])
% Do something to the matrix.
mRot = flipud(m)
% Do the same thing to the linear indexes.
newLinearIndexLocations = flipud(linearIndexes)