matlab中的acumarray命令
我想执行一个计算,通常我是通过以下方式对循环进行计算的:matlab中的acumarray命令,matlab,accumarray,Matlab,Accumarray,我想执行一个计算,通常我是通过以下方式对循环进行计算的: % d is a vector with size of k. % X_ind, Z_ind is a vector with size k. %(have indexes only in the range of m*n) % c is a vector with size k. % each value of d(i) , c(i) (i=1:k) mataches the indexes X_ind(i),Z_ind(i) % **
% d is a vector with size of k.
% X_ind, Z_ind is a vector with size k. %(have indexes only in the range of m*n)
% c is a vector with size k.
% each value of d(i) , c(i) (i=1:k) mataches the indexes X_ind(i),Z_ind(i)
% **In General**:
% N represents amplitudes (some negative,some positive) that sum up.
% M(:,:,1) reperesents a result that is weighted with M(:,:,2)
% M(:,:,1) and M(:,:,2) can be separate matrices
k=length(d); %also k=length(c), k=length(X_ind), k=length(Z_ind)
n=512 % can be any number
m=256 % can be any number
M=zeros(m,n);
N=zeros(m,n,2)+eps;
for i=1:(m*n)
x=X_ind(i);
z=Z_ind(i);
M(z,x)=M(z,x)+d(n);
a=abs(M(z,x)); % a is a scalar
N(z,x,1)=(N(z,x,1).*N(z,x,2)+c(n)*a)/(N(z,x,2)+a);
N(z,x,2)=N(z,x,2)+a;
end
而是使用accumarray命令:
M=accumarray([Z_ind X_ind], d];
N=???
有人能指出如何用这种方法计算N吗?你能提供变量的更多细节吗?例如,d是标量还是数组?您将其用作标量形式的
zero
的参数,然后将其作为数组形式的d(n)
索引。还有,你有你要计算的东西的数学表达式吗?你不能用现在的形式来计算。N是基于部分累加的M数组分配的(假设X_ind和Z_ind可以有重复的条目-此时您将使用accumarray)。一旦执行M=accumarray(),您就没有此信息。N赋值是否应该在外部,在另一个循环中?或者X_ind/Z_int中的索引没有重复?为什么不描述N
应该代表什么,而不是简单地转储代码,让我们试着找出它。@Richante,我补充了注释,d是数组(tnx),数学表达式只是简单的加权平均值。@angainor,X_ind和Z_ind在M和N的大小范围内有很多重复索引,循环非常长,在这个特定的计算中必须避免使用它。我添加了注释,请重新查看问题。