开发者

Generalize this matlab code for non-square matrices

开发者 https://www.devze.com 2023-03-04 23:35 出处:网络
I am working on some fourier transform code in matlab, and have come across the following: xx = meshgrid(1:N);

I am working on some fourier transform code in matlab, and have come across the following:

xx = meshgrid(1:N);
% Center on DC
xx = xx - dcN;
% normalize dynamic range from -1 to 1
xx = xx./max(abs(xx(:)));
% form y coordinate from negative transpose of x coordinate (maintains symmetry about DC)
yy = -xx';
% compute the related radius of the x/y coordinates centered on DC
rr = sqrt(xx.^2 + yy.^2);

How can I generalize this for non-square matrices? This code is assuming my matrix is square, so dcN is the center of the square matrix (in other words, with 11x11, dcN = 6).

The math doesnt work out for that yy variable when the transpose is taken for a non-square matrix.

I have tried to figure out if I can make a meshgrid going from "top to bottom" ins开发者_StackOverflow社区tead of left to right - but I havent been able to figure taht out either.

Thanks


I have tried to figure out if I can make a meshgrid going from "top to bottom" instead of left to right - but I havent been able to figure taht out either.

>> N=5

N =

     5

>> rot90(meshgrid(N:-1:1))

ans =

     1     1     1     1     1
     2     2     2     2     2
     3     3     3     3     3
     4     4     4     4     4
     5     5     5     5     5


From your question I guess that you want to find rr, i.e. the distance of any element in the matrix from the center.

If you want this for a M-by-N array, you'd do the following

%# note that using meshgrid instead of ndgrid will swap xx and yy
[xx,yy] = ndgrid(-(M-1)/2:(M-1)/2,-(N-1)/2:(N-1)/2);

%# normalize to the max of xx,yy
nrm = max((M-1)/2,(N-1)/2);
xx = xx./nrm; 
yy = yy./nrm;

rr = sqrt(xx.^2+yy.^2)
0

精彩评论

暂无评论...
验证码 换一张
取 消