将此 matlab 代码推广到非方阵

发布于 2024-11-06 04:14:28 字数 544 浏览 3 评论 0原文

我正在 matlab 中研究一些傅里叶变换代码,并遇到了以下问题:

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);

如何将其推广到非方阵?此代码假设我的矩阵是方阵,因此 dcN 是方阵的中心(换句话说,对于 11x11,dcN = 6)。

当对非方阵进行转置时,该 yy 变量的数学运算不起作用。

我试图弄清楚是否可以制作一个从“上到下”而不是从左到右的网格 - 但我也无法弄清楚。

谢谢

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" instead of left to right - but I havent been able to figure taht out either.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

变身佩奇 2024-11-13 04:14:28

我试图弄清楚我是否可以
制作一个从“顶部到
底部”而不是从左到右 - 但是
我还没弄清楚
要么。

>> 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

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
相守太难 2024-11-13 04:14:28

从你的问题来看,我猜你想找到 rr,即矩阵中任何元素距中心的距离。

如果您希望将其用于 M×N 数组,您可以执行以下操作

%# 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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文