在matlab中旋转矩阵后的网格高斯

时间:2013-10-22 23:34:57

标签: matlab matrix rotation mesh gaussian

我试图通过将旋转矩阵应用于X,Y坐标然后将其绘制为matlab中的网格来旋转高斯(或原始高斯的导数)的导数,但我正在遇到问题是该图每次只旋转90度,并且对于所有n * pi点,根本没有出现网格。我想知道我做错了什么,希望有人能发现我的错误。我对matlab很新,所以请原谅我,如果代码不漂亮的话。谢谢!

这是我的代码:

sigma = 4;
[x,y] = deal(-3*sigma:.5:3*sigma);
[X,Y] = meshgrid(x,y);
B = [transpose(x) transpose(y)];

for i=1:1:16
figure(i);
theta = i*pi/8;
rotation = [cos(theta) sin(theta); -sin(theta) cos(theta)];
A = B * rotation;
[x_new, y_new] = meshgrid(A(:,1)', A(:,2)');
mesh(x_new, y_new, dgauss_x(x_new, y_new, sigma));
end

function f = dgauss_x(x, y, sigma)
%first order derivative of Gaussian
f = -x .* gaussian(x, y, sigma) ./ sigma^2;

function f = gaussian(x, y, sigma)
f = exp(-(x .^ 2 + y .^ 2)/(2*sigma^2)) / (sqrt(2*pi*(sigma^2)));

1 个答案:

答案 0 :(得分:0)

我明白了。这是一个愚蠢的错误。基本上我必须为X和Y创建一个网格网格,将其重塑为X和Y的Nx2矩阵。应用旋转矩阵,然后重新整形并应用高斯。 代码如下(可以清理):

for i=1:1:16
figure(i);
theta = i*pi/8;
rotation = [cos(theta) -sin(theta); sin(theta) cos(theta)];
X1 = reshape(X, length(x)*length(x), 1);
Y1 = reshape(Y, length(y)*length(y), 1);
B2 = [X1 Y1];
A = B2 * rotation;
X1 = A(:,1);
X1 = reshape(X1, length(x), length(x));
Y1 = A(:,2);
Y1 = reshape(Y1, length(y), length(y));
mesh(X1, Y1, dgauss_x(X1, Y1, sigma));
end