我想通过以下方式执行通常用循环执行的计算:
% 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?