% merge_update( tr, j, f, p, path_index, el_index)
% - update kalman filters with merged paths
%
function merge_update( tr, j, f, p, path_index, el_index)

global DEBUG;
global K;
global N;
global M;
global kxv;

if( K > 1)
  error('merge_update, K > 1 not yet implemented');
end

p = p / sum(p);	% normalize probabilities

nf = length(f);

% Note: changed loop limit from j to j-1 for new merging scheme, RP 12 Feb. 2000
% because kxv values do not exist for el_index values > L
%
for i = (tr+1):j-1
  ii = i - tr;
% i, ii
  r = zeros(4,1);
  for n = nf:-1:1
    jj = [1:4] + (el_index(ii,f(n))-1)*4;
    i1 = 1 + ((i-1)*M + (path_index(ii,f(n))-1))*K;
% n, jj, i1
    r = r + p(n)*kxv(jj,i1);
  end
  kxv(jj,i1) = r;
end
