function [varargout] = display_layer( Pats, varargin )
%
% display_layer -- display the layer in the Neocognitoron
%
% Usage:
%    h = display_layer( Pats, [nrow figure_no] )
%     
%    layer: layer in the Neocognitron, which is 3dimensional array.
%    nrow: number of patterns in horizontal axis
%    figure_no: figure display
%
nrow = 8;
[ dummy argc ] = size( varargin );

if( argc >= 1 )
  nrow = varargin{1};
end
if( argc >= 2 )
  figure_no = figure( varargin{2} );
else
  figure_no = figure()
end

[W H P] = size( Pats );

m = nrow;
n = floor( P / nrow );

if( mod( P, nrow ) > 0 )
  n = n+1;
end

mgn = 2;     % margin size

pmax = max( Pats(:) );
pmin = -0.2 * pmax;
ary = -0.8*pmax * ones( mgn + m * (H+mgn), mgn + n * (W+mgn) );

k=1;
for j=1:n
  for i=1:m
    clim = max(max(abs(Pats(:,:,k))));
    ary( mgn+(i-1)*(H+mgn)+[1:H], mgn+(j-1)*(W+mgn)+[1:W])=...
	Pats(:,:,k)';
    k = k+1;
    if( k > P ), break, end
  end
end
colormap( jet );
h = imagesc( ary, 'EraseMode', 'none', [pmin  pmax]);
axis image off
varargout{1} = ary;
varargout{2} = figure_no;
 

