Calculate compound amount in matlab

With the following variables:

m = 1:4; n = 1:32;
phi = linspace(0, 2*pi, 100);
theta = linspace(-pi, pi, 50);

S_mn = <a 4x32 coefficient matrix, corresponding to m and n>;

how can I calculate the sum over m and n from S_mn*exp(1i*(m*theta + n*phi)), i.e.

$ \ sum_m \ sum_n S_mn exp (i (m \ theta + n \ phi))

I thought about things like

[m, n] = meshgrid(m,n);
[theta, phi] = meshgrid(theta,phi);
r_mn = S_mn.*exp(1i*(m.*theta + n.*phi));
thesum = sum(r_mn(:));

but it takes thetaand phihave the same number of elements, such as mand n, and it returns to me only one element in the reverse order - I need a matrix of size meshgrid(theta,phi), regardless of the size thetaand phi(that I want to be able to estimate the amount as a function of thetaand phi).

How to do it in matlab?

0
source share
3 answers

Since I do not know what S ...

S = randn(4,32);

[m,n] = ndgrid(1:4,1:32);
fun = @(theta,phi) sum(sum(S.*exp(sqrt(-1)*(m*theta + n*phi))));

Works great for me.

fun(pi,3*pi/2)
ans =
          -15.8643373238676 -      1.45785698818839i

phi theta, . , . . WTP?

, meshgrid, ndgrid ? , , bsxfun, .

[m,n,theta,phi] = ndgrid(1:4,1:32,linspace(-pi, pi, 50),linspace(0, 2*pi, 100));
res = bsxfun(@times,S,exp(sqrt(-1)*(m.*theta + n.*phi)));
res = squeeze(sum(sum(res,1),2));

, . .07 . 0,05, , bsxfun.

m = (1:4)';
n = 1:32;
[theta,phi] = ndgrid(linspace(-pi, pi, 50),linspace(0, 2*pi, 100));
theta = reshape(theta,[1,1,size(theta)]);
phi = reshape(phi,size(theta));
res = bsxfun(@plus,bsxfun(@times,m,theta*sqrt(-1)),bsxfun(@times,n,phi*sqrt(-1)));
res = bsxfun(@times,S,exp(res));
res = squeeze(sum(sum(res,1),2));

2000 , 100 . WTP? .

+1

:

size_m = size(m);
size_n = size(n);
size_theta = size(theta);
size_phi = size(phi);

ngrid, :

[theta, phi, m, n] = ngrid(theta, phi, m, n)

4 ( : theta, phi, m, n). :

m.*theta + n.*phi

S_mn 4 size_theta, size_phi, size_m, size_n :

S_tpmn = repmat(S_mn, [size_theta size_phi size_m size_n]);

:

aux_sum = S_tpmn.*exp(1i*(m.*theta + n.*phi));

, 2 (m n), size_theta size_phi:

final_sum = sum(sum(aux_sum, 4), 3);

. Matlab , , .

0

.

- (-handle), theta phi, arrayfun . - , .

arrayfun:

[m, n] = meshgrid(m,n);

sumHandle = @(theta,phi)sum(reshape(...
    S_mn.*exp(1i(m*theta + n*phi)),...
    [],1)) 

[theta, phi] = meshgrid(theta,phi);

sumAsFunOfThetaPhi = arrayfun(sumHandle,theta,phi);

:

[m, n] = meshgrid(m,n);
m = permute(m(:),[2 4 1 3]); %# vector along dim 3
n = permute(n(:),[2 3 4 1]); %# vector along dim 4

S_mn = repmat( permute(S_mn,[3 4 1 2]), length(theta),length(phi));

theta = theta(:); %# vector along dim 1 (phi is along dim 2 b/c of linspace)

fullSum = S_mn.* exp( 1i*(...
    bsxfun(@plus,...
       bsxfun(@times, m, theta),...
       bsxfun(@times, n, phi),...
    )));

sumAsFunOfThetaPhi = sum(sum( fullSum, 3),4);
0
source

All Articles