function A = argmax(X,D)
%    ARGMAX Return the indices of maximum elements of an array
%
% A = ARGMAX(X) returns the indices of the largest elements along
% different dimensions of an array. 
%
% If X is a vector, it returns the index of the largest element.
%
% If X is an array, it returns the indices of the largest element
% in each column in a row vector.
%
% If X is a multidimensional array, it treats the values along the
% first non-singleton dimension as vectors, returning the index of
% the  maximum value of each vector.
%
% A = ARGMAX(X,D) returns the indices of the largest elements along
% dimension D.
%
% Returns the index of the first element found in case of ties.

  
% Jerod Weinman 
% jerod@acm.org
% (c) 2008

% $Id: argmax.m 1 2009-05-07 13:41:00Z weinman $
 
% Jerod Weinman
% jerod@acm.org
% /home/data/svn/vision/tools/package/GPL_STATEMENT
 
  
  
  if nargin==1 & ndims(X)==2 & any(size(X)==1) % vector
	[M, A] = max(X);
  else
	if nargin==1
	  D = min(find(size(X)~=1)); % first non-singleton dimension
	elseif D<1 | D>ndims(X)
      error('Invalid dimension %d',D);
	end;
    
	[M,A] = max(X,[],D);
    
  end;
