function h = display_coef( A, B, varargin )
%
% display_coef -- display the loaded connections from Uc[l-1] to Us[l] using A and B
%
% Usage:
%    h = display_pat( A, B )
%     
%    A: Excitatory connections expressed by A. This data expressed by cell matrix, 
%       which consists of coefficiets double valued matrix.
%    B: Inhibitory connections expressed by B. This data expressed by double arrayed values.
%
%
%    A means
%          --------------------------------> No. of Uc
%      |   [matrix]  [matrix]   [matrix]  ...
%      |
%      |   [matrix]  [matrix]   [matrix]  ...
%      |
%      |   [matrix]  [matrix]   [matrix]  ...
%      |   
%      |      :         :          :
%      v
%    No. of Us
%
%      A{ks, kc} means connection matrix from Uc[l-1][kc] to Us[l][ks], 
%      Thus A{ks, kc} == NULL there exists no connections
%

[dummy argc] = size( varargin );
if( argc >= 1 )
  figure_no = figure( varargin{1} );
else
  figure_no = figure( 1 );
end

[nKs nKc] = size( A );
Bmax = max( B );
for n = 1:nKs
  for m = 1:nKc
    [NaH NaW] = size( A{n, m} );
    if ( NaH == NaW ) && ( NaW > 0 ), break, end
  end
end

m = nKc;
n = nKs;
mgn = 2;     % margin size

pmax = 0;
max_i = [];
for i=1:nKs
  clim = B(i);
  max_i(i) = 0;
  for j=1:nKc
    [NaH NaW] = size( A{i,j} );
    if( NaH == NaW ) && ( NaH > 0 )
      max_i(i) = max( max_i(i), max(A{i,j}(:)/clim) );
    end
    pmax = max( max_i(i), pmax );
  end
end

pmin = -0.5 * pmax;
ary = pmin * ones( mgn + nKs * (NaH+mgn), mgn + nKc * (NaH+mgn) );


for i=1:nKs
  clim = B(i);
  for j=1:nKc
    [NaH NaW] = size( A{i, j} );
    if( NaH == NaW ) && ( NaH > 0 )
      ary( mgn+(i-1)*(NaH+mgn)+[1:NaH], mgn+(j-1)*(NaW+mgn)+[1:NaW])=...
	  ((A{i, j} ./ clim)/max_i(i))';
    end
  end
end
colormap( bone );
%h = imagesc( ary, 'EraseMode', 'none', [-1 1]);
imagesc( ary, 'EraseMode', 'none', [min(ary(:)) max(ary(:))]);
h = ary;
axis image off
 

