How to track indices of a matrix after a transformation

7 views (last 30 days)
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 9x2 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 9x2 matrix which stores the row col indices of the entries after transformation)
I would appreciate any help in this regard.

Accepted Answer

Image Analyst
Image Analyst on 26 May 2013
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)

More Answers (2)

Azzi Abdelmalek
Azzi Abdelmalek on 26 May 2013
A = [1 2 3; 4 5 6;7 8 9]
idx=1:numel(A) % the index of A
B=rot90(A);
idx1=reshape(idx,size(A))
new_idx=rot90(idx1);
new_idx=new_idx(:);

Benoit Espinola
Benoit Espinola on 4 Apr 2019
I had the same issue and cameup with this:
M = [1 2 3; 4 5 6; 7 8 9; 10 11 12];
m = reshape(M, numel(M),1);
x = reshape(repmat(1:size(M,1) ,size(M,2),1), numel(M),1);
y = reshape(repmat(1:size(M,2),size(M,1),1)', numel(M),1);
In my problem, I need to keep track of the original indices of the values in the original matrix (as they were keys for something else). Here x and y keep track of the original position, so m(1) was at position x(1) for columns and y(1) for rows.
I am sure there must be a better way to do it and I am not sure how this behaves in matrices with more dimensions.

Community Treasure Hunt

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

Start Hunting!