Create matrix with elements representing distance from centre of matrix

25 views (last 30 days)
Hello. If i have a 3x3, 5x5 or 7x7 matrix or generally a nxn matrix where n is odd, i want each element to represent the distance from the centre of the matrix. I.e the top left element in a 3x3 matrix to represent up one row and left 1 column. I.e (1,-1).
I then want to unravel this matrix into a 1D vector but in a serpentine fashion rather than raster scan

Accepted Answer

Image Analyst
Image Analyst on 31 Jan 2021
Jason if you want a for loop way of doing it, you can do this:
rows = 3;
columns = 4;
distances = zeros(rows, columns);
midRow = mean([1, rows])
midCol = mean([1, columns])
for col = 1 : columns
for row = 1 : rows
distances(row, col) = sqrt((row - midRow) .^ 2 + (col - midCol) .^ 2);
end
end
distances
Some results:
For 3,4:
distances =
1.8028 1.118 1.118 1.8028
1.5 0.5 0.5 1.5
1.8028 1.118 1.118 1.8028
For 3,3
distances =
1.4142 1 1.4142
1 0 1
1.4142 1 1.4142
For 4,4
distances =
2.1213 1.5811 1.5811 2.1213
1.5811 0.70711 0.70711 1.5811
1.5811 0.70711 0.70711 1.5811
2.1213 1.5811 1.5811 2.1213
  2 Comments
Jason
Jason on 31 Jan 2021
Hi IA. Im moving a microscope stage so need both the x and y distances rather than r.
Image Analyst
Image Analyst on 31 Jan 2021
Jason, it's so trivial, I'm sure you did it by now, but for what it's worth:
rows = 3;
columns = 4;
deltax = zeros(rows, columns);
deltay = zeros(rows, columns);
midRow = mean([1, rows])
midCol = mean([1, columns])
for col = 1 : columns
for row = 1 : rows
deltax(row, col) = col - midCol;
deltay(row, col) = row - midRow;
end
end
deltax
deltay
For the number of rows and columns shown, this is what you see.
midRow =
2
midCol =
2.5
deltax =
-1.5 -0.5 0.5 1.5
-1.5 -0.5 0.5 1.5
-1.5 -0.5 0.5 1.5
deltay =
-1 -1 -1 -1
0 0 0 0
1 1 1 1
But you might want to use meshgrid(). Just make sure you have the center location properly located with the right value. Like, if you have an even number of rows, the "middle" will not be exactly at a particular row, but in between existing rows.

Sign in to comment.

More Answers (2)

Jason
Jason on 30 Jan 2021
Edited: Jason on 30 Jan 2021
Here's my attempt - not sure if its the best way:
n=5; % n must be odd (nxn matrix)
y=1:n^2 % create the numbers to put into a matrix
A=reshape(y,n,n); % create the matrix
A=A'
%n=length(y);
for i=2:2:n
r=A(i,:);
r=fliplr(r); %Flip every even row, create serpentine pattern
A(i,:)=r;
end
A
M=[];
for i=1:n^2
[r,c]=find(A==i);
M(i,1)=r;
M(i,2)=c;
end
M
%Get middle element position
m=ceil(n/2)
%subtract from each M
M(:,1)=M(:,1)-m
M(:,2)=M(:,2)-m
M % This is now a vector of positions relative to the origin

darova
darova on 31 Jan 2021
What about this?
m = 5;
n = (m-1)/2;
[X,Y] = meshgrid(-n:n);
A = sqrt(X.^2+Y.^2)
surf(A)

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Tags

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!