创建具有系数和可变项数的二维余弦波矩阵

时间:2019-04-26 00:16:18

标签: matlab

发布this question yesterday后,我意识到我想为表格的每个条目创建n×n维不同的相似矩阵

a * cos(j * x + k * y)

其中a是系数的向量; j,x,k和y是从0到n-1的索引。

例如,如果n = 4

>> n = 4;
>> x = 0:(n-1);
>> y = 0:(n-1);
>> [x,y] = meshgrid(x,y)
x =

   0   1   2   3
   0   1   2   3
   0   1   2   3
   0   1   2   3

y =

   0   0   0   0
   1   1   1   1
   2   2   2   2
   3   3   3   3

结果矩阵将包含16个条目,可以通过以下函数进行计算:

f = @(x, y,a0,a1,a2,a3,b0,b1,b2,b3,c0,c1,c2,c3,d0,d1,d2,d3)... 
a0*cos(0*x + 0*y) + a1*cos(0*x + 1*y) +...
a2*cos(0*x + 2*y) + a3*cos(0*x + 3*y) + ...
b0*cos(1*x + 0*y) + b1*cos(1*x + 1*y) + ...
b2*cos(1*x + 2*y) + b3*cos(1*x + 3*y) + ...
c0*cos(2*x + 1*y) + c1*cos(2*x + 1*y) + ...
c2*cos(2*x + 2*y) + c3*cos(2*x + 3*y) + ...
d0*cos(3*x + 1*y) + d1*cos(3*x + 1*y) + ...
d2*cos(3*x + 2*y) + d3*cos(3*x + 3*y)

当然,除了需要在余弦前面提供系数外,例如,如果要生成256 x 256矩阵,则键入所有这些余弦表达式是不可行的。

我玩过for循环,但是我没有得到我想要的东西,关于函数中独立索引循环的数量出现错误。

1 个答案:

答案 0 :(得分:3)

编辑:我编辑了初始答案,并添加了Guille's comment中给出的想法。 (首先没有看到它...)请查看更新的代码。


Smee again。您可以像这样组合匿名函数/函数句柄:

f = @(x) sin(x);
g = @(x) cos(x);
h = @(x) f(x) + g(x);

尽管如此,我想还是有必要将您的函数(句柄)f的封装封装到一些“真实”的MATLAB函数中,请参见以下代码:

function f = setupF(n, a)

  % Possibly, add some checks, e.g. for numel(a) == n^2, and so on.

  % Initialize function handle.  
  f = @(x, y) 0;
  ind = 0;

  % Iteratively add cosine parts. 
  for ii = 0:(n-1)
    for jj = 0:(n-1)
      ind = ind + 1;
      g = @(x, y) a(ind) * cos(ii * x + jj * y);
      f = @(x, y) f(x, y) + g(x, y); 
    end
  end

end

以下是一个测试脚本:

% Set up parameters.
n = 3;
a = reshape(1:n^2, n, n);

% Set up f(x, y) by function.
f = setupF(n, a);

% Set up f explicitly, as g(x, y). 
g = @(x, y) ...
  a(1) * cos(0*x + 0*y) + ...
  a(2) * cos(0*x + 1*y) + ...
  a(3) * cos(0*x + 2*y) + ...
  a(4) * cos(1*x + 0*y) + ...
  a(5) * cos(1*x + 1*y) + ...
  a(6) * cos(1*x + 2*y) + ...
  a(7) * cos(2*x + 0*y) + ...
  a(8) * cos(2*x + 1*y) + ...
  a(9) * cos(2*x + 2*y);

% Set up f(x, y) by vectorization, as h(x, y).
I = 0:(n-1);
J = 0:(n-1);
[I, J] = meshgrid(I, J);
h = @(x, y, n, a) sum(reshape(a .* cos(x * I + y * J), n^2, 1));
h = @(x, y, n, a) arrayfun(@(x, y) h(x, y, n, a), x, y);

% Set up test data.
x = linspace(0, 2*pi, 5);
y = linspace(0, 2*pi, 5);
[X, Y] = meshgrid(x, y);

% Compare outputs.
fRet = f(X, Y)
gRet = g(X, Y)
hRet = h(X, Y, n, a)

然后,输出:

fRet =
   45.0000  -18.0000   15.0000  -18.0000   45.0000
   -6.0000   -5.0000   -2.0000    5.0000   -6.0000
   15.0000   -6.0000    5.0000   -6.0000   15.0000
   -6.0000    5.0000   -2.0000   -5.0000   -6.0000
   45.0000  -18.0000   15.0000  -18.0000   45.0000

gRet =
   45.0000  -18.0000   15.0000  -18.0000   45.0000
   -6.0000   -5.0000   -2.0000    5.0000   -6.0000
   15.0000   -6.0000    5.0000   -6.0000   15.0000
   -6.0000    5.0000   -2.0000   -5.0000   -6.0000
   45.0000  -18.0000   15.0000  -18.0000   45.0000

hRet =
   45.0000  -18.0000   15.0000  -18.0000   45.0000
   -6.0000   -5.0000   -2.0000    5.0000   -6.0000
   15.0000   -6.0000    5.0000   -6.0000   15.0000
   -6.0000    5.0000   -2.0000   -5.0000   -6.0000
   45.0000  -18.0000   15.0000  -18.0000   45.0000

当然,就性能而言,“矢量化”方法是有好处的:

Output