% plots for x1 and x2 vs. L in various ways, for K == 1

clf;

if( t < 3)

  XX = zeros(N,L);
  YY = zeros(N,L);
  ZZ = zeros(N,L);

  for i=1:L
    a=get_kxv(i);
    XX(:,i) = a(:,1);
    YY(:,i) = a(:,3);
%    ZZ(:,i) = -w_min(i) * ones(N,1);
    ZZ(:,i) = i * ones(N,1);
  end

end


if( t == 1)
  plot(XY(1,:),XY(2,:),'x');
  hold on
  for i=1:L
    plot(XX(:,i),YY(:,i))
    text(XX(N,i)+i/30,YY(N,i)+i/60,sprintf('%d',i));
  end
  
  xlabel 'X';
  ylabel 'Y';
  title 'Single Track: best paths';

  p = exp(-(w_min-min(w_min)));
  p = p / sum(p);

  % E[path]
  %
  XXX = XX*p;
  YYY = YY*p;

  plot( XXX, YYY, '-go');

  % true paths with noise
  %
  NN = sum(n1);
  XT = zeros(NN,1);
  YT = zeros(NN,1);
  j = 0;
  for i=1:N
    if( n1(i))
      j = j + 1;
      k = 1 + XY_offset(i);
      XT(j) = XY(1,k); 
      YT(j) = XY(2,k); 
    end
  end

  plot( XT, YT, '-r*');

  if( exist('n2'))
    NN = sum(n2);
    XT = zeros(NN,1);
    YT = zeros(NN,1);
    j = 0;
    for i=1:N
      if( n2(i))
        j = j + 1;
        k = n1(i) + 1 + XY_offset(i);
        XT(j) = XY(1,k); 
        YT(j) = XY(2,k); 
      end
    end
  end

  plot( XT, YT, '-r*');

  text(3,0.5,'o  E[track]')
  text(3,0.25,'*  True noisy track')

  hold off

elseif( t == 2)

%  view(37.5,30)
  view(-37.5,30)

  xlabel 'X'
  ylabel 'Y'
  % zlabel 'log(P)'
  zlabel 'L index'

  axis([-1 5 -1 5 1 L])

%  mesh(XX,YY,ZZ)

  hold on;
  grid on;

  for i=1:L
    line(XX(:,i),YY(:,i),ZZ(:,i));
  end

  hold off;

elseif( t == 3)

  % set el value before running this section

  view(37.5,30);

  hold on;

  grid on;

  axis([-1 5 -1 5 1 N])

  xlabel 'X'
  ylabel 'Y'
  zlabel 'Time'

  title(sprintf('el = %d', el));

  for NN = 1:N

    wt = w_save( :, (NN-1)*M+[1:M]);
  
    % find el'th-best path at time NN
  
    [tmp ii_min] = sort(wt(:));
  
    ii_min = ii_min(1:el);
  
    i_min = sort_cols(w,ii_min);
    l_min = sort_rows(w,ii_min);
  
    i_min = i_min(el);
    l_min = l_min(el);

    ii_min = ii_min(el);

    wt_min = wt(:);
    wt_min = wt_min(ii_min);
  
    if( wt_min ~= HUGE_VAL)

      v_cur = v(:,1+NN*M:(NN+1)*M);
      vl_cur = vl(:,1+NN*M:(NN+1)*M);
    
      NN_path_index = zeros(1,NN);
      NN_el_index = NN_path_index;
    
      for j = NN:-1:1  % create path_index
    
        NN_path_index(j) = i_min;
        NN_el_index(j) = l_min;
    
        ii_min = v_cur( l_min, i_min);
        ll_min = vl_cur( l_min, i_min);
      
        i_min = ii_min;
        l_min = ll_min;
      
        v_cur = v(:,1+(j-1)*M:j*M);
        vl_cur = vl(:,1+(j-1)*M:j*M);
      
      end % j
    
%      NN_path_index = NN_path_index';
%      NN_el_index = NN_el_index';
    
% [NN_path_index, NN_el_index]

      a = NN_get_kxv( 1, NN_path_index, NN_el_index);
  
      XX = a(:,1);
      YY = a(:,3);
      ZZ = NN * ones(NN,1);
  
      line(XX,YY,ZZ);
  
      text(XX(NN),YY(NN),ZZ(NN),sprintf('%d',NN));
  
    end
  
  end

  hold off;

end
