0

I want to perform a calculation that usually I am doing with a loop in the following way:

% 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

and instead use accumarray command:

M=accumarray([Z_ind X_ind], d];

N=???

can some one point out how to calculte N in that way?

OmG
  • 18,337
  • 10
  • 57
  • 90
jarhead
  • 1,821
  • 4
  • 26
  • 46
  • 4
    Could you provide more detail on what the variables are? For example, is d a scalar or an array? You are using it as an argument to `zeros` like a scalar, but then indexing it `d(n)` like an array. Also, do you have a mathematical expression of the thing you're trying to calculate? – Richante Sep 11 '12 at 09:32
  • 1
    You can not do it in the current form. N is assigned based on partially accumulated M array (assuming X_ind and Z_ind can have repeated entries - thats when you would use accumarray). You don't have this information once you do M=accumarray(). Should the N assignment should be outside, in another loop? Or the indices in X_ind/Z_int are not repeated? – angainor Sep 11 '12 at 11:19
  • Why not describe what `N` should represent rather than simply dumping code and asking us to try and figure it out. – slayton Sep 11 '12 at 14:43
  • @Richante, I added commentary, d is an array (tnx), the mathematical expression is just a simple wighted average. – jarhead Sep 12 '12 at 01:29
  • @angainor, X_ind and Z_ind have a lot of repeated indexes in the range of the size of M and N, the loop is very long, it is essential to avoid using it in this speciefic calculation.I added commentary, please reveiew the question again. – jarhead Sep 12 '12 at 01:34

0 Answers0