在Matlab中计算棘手的总和 [英] Calculate a tricky sum in Matlab
问题描述
具有以下变量:
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>;
如何计算S_mn*exp(1i*(m*theta + n*phi))
的 m 和 n 上的总和,即
>
我想到了
[m, n] = meshgrid(m,n);
[theta, phi] = meshgrid(theta,phi);
r_mn = S_mn.*exp(1i*(m.*theta + n.*phi));
thesum = sum(r_mn(:));
,但这需要theta
和phi
具有与m
和n
相同的元素数量,并且它只给我一个元素作为回报-我想要一个尺寸为meshgrid(theta,phi)
的矩阵,无论theta
和phi
的大小如何(即,我希望能够将和作为theta
和phi
的函数进行求值).
如何在matlab中进行此计算?
因为我不知道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))));
对我来说很好.
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));
或者这样做,这会更快一些.之前的计算花了我的机器0.07秒.最后一个使用.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(psize(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?喝杯咖啡放松一下.
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 do I compute the sum over m and n of S_mn*exp(1i*(m*theta + n*phi))
, i.e.
I've thought of 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 that requires theta
and phi
to have the same number of elements as m
and n
, and it gives me just one element in return - I want a matrix the the size of meshgrid(theta,phi)
, regardless of the sizes of theta
and phi
(i.e. I want to be able to evaluate the sum as a function of theta
and phi
).
How do I do this calculation in matlab?
Since I don't know what S is...
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 fine for me.
fun(pi,3*pi/2)
ans =
-15.8643373238676 - 1.45785698818839i
If you now wish to do this for a large set of values phi and theta, a pair of loops now are the trivial solution. Or, you can do it all in one computation, although the arrays will get larger. Still not hard. WTP?
You do realize that both meshgrid and ndgrid take more than just two arguments? So it is time to learn how to use bsxfun, and then squeeze.
[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));
Or do this, which will be a bit faster. The previous computation took my machine .07 seconds. This last one took .05, so some savings by using bsxfun heavily.
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));
If you need to do the above 2000 times, so it should take 100 seconds to do. WTP? Get some coffee and relax.
这篇关于在Matlab中计算棘手的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!