function h = matrix_graph(a,rc,mrk,bounds)
%
%  Illustrates the rows and columns of a real-valued matrix.
%
%  Input
%    a       matrix
%    rs      character code ; tells if the illustration
%                             is by rows or by columns
%            'r': by rows (default)
%            'c': by columns
%    mrk     the marker to be used (default '+')
%    bounds  [lower_bound upper_bound] 
%            The upper and lower bounds for the y-coordinates,
%            default [floor(10*min(min(a)))/10  ceil(10*max(max(a)))/10]
%
%  Output
%    h       array of handles to the arising line objects
%
%  Author:   Erkki Heikkola, emsh@mit.jyu.fi
%  Date:     November 8, 2000
%

error(nargchk(1,4,nargin));
if nargin<4,
   bounds = [floor(10*min(min(a)))/10  ceil(10*max(max(a)))/10];
   if nargin<3,
      mrk = '+';
      if nargin<2,
         rc = 'r';
      end
   end
end

if bounds(2)<=bounds(1),
   error('Invalid bounds for matrix_graph.');
end

if ~isreal(a),
   error('Matrix needs to be real-valued');
end
[N M] = size(a);

h = [];
if rc=='c',
%
% Matrix entries by columns
% - each column in one subplot
% - one row of subplots
%
   for k=1:M,
      subplot(1,M,k), h = [ h plot(1:N,a(:,k),mrk) ];
      fig_header = strcat(num2str(k),'. column');
      title(fig_header);
      set(gca,'XLim',[1 N]);
      set(gca,'XTick',1:N);
      set(gca,'YLim',bounds);
   end
elseif rc=='r',
%
% Matrix entries by rows
% - each row in one subplot
% - one column of subplots 
%
   for k=1:N,
      subplot(N,1,k), h = [ h plot(1:M,a(k,:),mrk) ];
      fig_header = strcat(num2str(k),'. row');
      title(fig_header);
      set(gca,'XLim',[1 M]);
      set(gca,'XTick',1:M);
      set(gca,'YLim',bounds);
   end
else
   error('Illustration parameter rc not well-defined.');
end

% end of function
