$\begingroup$

In MatLab matrices, the indices are as follows:

(1,1) (1,2) (1,3) (2,1) (2,2) (2,3) (3,1) (3,2) (3,3) 

This is an example 3x3 matrix. In corresponding cartesian coordinate system, the representation would be:

(-1,1) (0,1) (1,1) (-1,0) (0,0) (1,0) (-1,1) (0,-1) (1,-1) 

Say, I have any square matrix with dimension-N, where N is odd. I need a generic transformation matrix such that I can get a vector as cartesian coordinates from matrix indices. Does such a function already exist? How should I go ahead in implementing this?

Thanks.

$\endgroup$1

3 Answers

$\begingroup$

The transformation of indices is the following:

$$ (x,y) = f(i,j) = \left( j-\frac{n+1}{2} ,-i + \frac{n+1}{2}\right) \ . $$

Here $i$ is the index for the rows, $j$ the one for the columns and $n$ the order of your square matrix.

$\endgroup$$\begingroup$

Interchange indices $i$ and $j$ in initial matrix, then flip it upside down to get the same orientation like a usual coordinate system and then subtract $(2,2)$ or $(\frac{n+1}{2},\frac{n+1}{2})$ in general to shift the center.

$\endgroup$$\begingroup$
clear all; clc; close all; % % % % % % % % Create array with '1' and mark the center with '0' % % % % % % % % % ones = ones(8,8); ones(4,5)=2; [x,y] = size(ones); for i=1:x for j=1:y if ones(i,j) == 2; index = [i j]; end end end newT1 = zeros(x,y); newT1(index(1,1),index(1,2)) = 5; [x,y] = size(ones); for i=1:x for j=1:y A(i,j) = int2str(i)+","+int2str(j); end end c = strsplit (A(index(1,1),index(1,2)),','); % % % % % % % % find Upper and Down limit % % % % % % % % % uplimit= strsplit(A(1,index(1,2)),','); dnlimit= strsplit(A(x,index(1,2)),','); rilimit= strsplit(A(index(1,1),y),','); lelimit= strsplit(A(index(1,1),1),','); % % % % % % % % find how many blocks are until the end % % % % % % % % % up = abs(str2num(c(1,1)) - str2num(uplimit(1,1))); down = abs(str2num(c(1,1)) - str2num(dnlimit(1,1))); right = abs(str2num(c(1,2)) - str2num(rilimit(1,2))); left = abs(str2num(c(1,2)) - str2num(lelimit(1,2))); % % % % % % % % Create X,Y axis of the cartesians % % % % % % % % % for i=1:up one = strsplit(A(index(1,1)-i,index(1,2)),','); A(index(1,1)-i,index(1,2)) = "0," + int2str(i); end for i=1:down one = strsplit(A(index(1,1)+i,index(1,2)),','); A(index(1,1)+i,index(1,2)) = "0,"+int2str(-i); end for i=1:right one = strsplit(A(index(1,1),index(1,2)+i),','); A(index(1,1),index(1,2)+i) = int2str(i) + ",0"; end for i=1:left one = strsplit(A(index(1,1),index(1,2)-i),','); A(index(1,1),index(1,2)-i) = int2str(-i) + ",0"; end % % % % % % % % Complete the matrices with the values % % % % % % % % % A(index(1,1),index(1,2)) = "0,0"; for i=1:up for j=1:right A(i,y-right+j) = int2str(j) +"," +int2str(up+1-i); end end for i=1:down for j=1:right A(up+i+1,y-right+j) =int2str(j)+","+int2str(0-i); end end for i=1:up for j=1:left A(i,j) = int2str(-left-1+j)+","+int2str(up+1-i); end end for i=1:down for j=1:left A(up+i+1,j) = int2str(-left-1+j)+","+int2str(0-i); end end A 

That's my solution for MATLAB :)

$\endgroup$

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy