viewmatrix

PURPOSE ^

Visualize 2d-matrices with colorful plots.

SYNOPSIS ^

function viewmatrix(x,c,alpha_value)

DESCRIPTION ^

 Visualize 2d-matrices with colorful plots.
 VIEWMATRIX   Visualize 2d-matrices with colorful plots. 
     VIEWMATRIX(X) visualizes a NxM matrix by plotting each matrix entry 
     as a colored square. The entry value itself is also displayed. Since 
     the digits of the number are not plotted as regular text but as 
     bitmaps, the method scales the text for any matrix size. 
 
     VIEWMATRIX(X,C) uses the matrix C as the color matrix. Size of C 
     should be equal to the size of X. 
 
     VIEWMATRIX(X,C,ALPHA_VALUE) uses the number in aplha_value to make 
     the background transparent. Alpha_value must lie between 0.0 and 1.0,  
     with 1.0 being opaque. Default value is 1.0. 
  
     Examples: 
          x = magic(5), viewmatrix(x); 
          x = magic(5), c = rand(size(x)), viewmatrix(x,c); 
          viewmatrix(magic(5), [], 0.3);

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 % Visualize 2d-matrices with colorful plots.
0002 function viewmatrix(x,c,alpha_value) 
0003 % VIEWMATRIX   Visualize 2d-matrices with colorful plots.
0004 %     VIEWMATRIX(X) visualizes a NxM matrix by plotting each matrix entry
0005 %     as a colored square. The entry value itself is also displayed. Since
0006 %     the digits of the number are not plotted as regular text but as
0007 %     bitmaps, the method scales the text for any matrix size.
0008 %
0009 %     VIEWMATRIX(X,C) uses the matrix C as the color matrix. Size of C
0010 %     should be equal to the size of X.
0011 %
0012 %     VIEWMATRIX(X,C,ALPHA_VALUE) uses the number in aplha_value to make
0013 %     the background transparent. Alpha_value must lie between 0.0 and 1.0,
0014 %     with 1.0 being opaque. Default value is 1.0.
0015 %
0016 %     Examples:
0017 %          x = magic(5), viewmatrix(x);
0018 %          x = magic(5), c = rand(size(x)), viewmatrix(x,c);
0019 %          viewmatrix(magic(5), [], 0.3);
0020  
0021 %  Author : Mirza Faisal Baig
0022 %  Version: 1.0
0023 %  Date   : January 22, 2004
0024  
0025 % Numbers defined as matrices to be diaplayed as textures
0026 bitmapdata = { ... 
0027          [0 0 0 0 0 0 0 0 0 0; 
0028           0 0 0 0 1 1 0 0 0 0; 
0029           0 0 0 1 1 1 0 0 0 0; 
0030           0 0 1 1 1 1 0 0 0 0; 
0031           0 1 1 0 1 1 0 0 0 0; 
0032           0 0 0 0 1 1 0 0 0 0; 
0033           0 0 0 0 1 1 0 0 0 0; 
0034           0 0 0 0 1 1 0 0 0 0; 
0035           0 0 0 0 1 1 0 0 0 0; 
0036           0 0 0 0 1 1 0 0 0 0; 
0037           0 0 0 0 1 1 0 0 0 0; 
0038           0 0 0 0 1 1 0 0 0 0; 
0039           0 0 0 0 1 1 0 0 0 0; 
0040           0 1 1 1 1 1 1 1 1 0; 
0041           0 0 0 0 0 0 0 0 0 0],  
0042   
0043          [0 0 0 0 0 0 0 0 0 0; 
0044           0 0 0 1 1 1 1 0 0 0; 
0045           0 0 1 1 0 0 1 1 0 0; 
0046           0 1 1 0 0 0 0 1 1 0; 
0047           0 1 1 0 0 0 0 1 1 0; 
0048           0 0 0 0 0 0 0 1 1 0; 
0049           0 0 0 0 0 0 0 1 1 0; 
0050           0 0 0 0 0 0 1 1 0 0; 
0051           0 0 0 0 1 1 1 0 0 0; 
0052           0 0 0 1 1 0 0 0 0 0; 
0053           0 0 1 1 0 0 0 0 0 0; 
0054           0 1 1 0 0 0 0 0 0 0; 
0055           0 1 1 0 0 0 0 0 0 0; 
0056           0 1 1 1 1 1 1 1 1 0; 
0057           0 0 0 0 0 0 0 0 0 0],  
0058   
0059          [0 0 0 0 0 0 0 0 0 0; 
0060           0 0 0 1 1 1 1 0 0 0; 
0061           0 0 1 1 0 0 1 1 0 0; 
0062           0 1 1 0 0 0 0 1 1 0; 
0063           0 1 1 0 0 0 0 1 1 0; 
0064           0 0 0 0 0 0 0 1 1 0; 
0065           0 0 0 0 0 0 1 1 0 0; 
0066           0 0 0 0 1 1 1 0 0 0; 
0067           0 0 0 0 0 0 1 1 0 0; 
0068           0 0 0 0 0 0 0 1 1 0; 
0069           0 1 1 0 0 0 0 1 1 0; 
0070           0 1 1 0 0 0 0 1 1 0; 
0071           0 0 1 1 0 0 1 1 0 0; 
0072           0 0 0 1 1 1 1 0 0 0; 
0073           0 0 0 0 0 0 0 0 0 0],  
0074        
0075          [0 0 0 0 0 0 0 0 0 0; 
0076           0 0 0 0 0 0 0 1 0 0; 
0077           0 0 0 0 0 0 1 1 0 0; 
0078           0 0 0 0 0 1 1 1 0 0; 
0079           0 0 0 0 1 1 1 1 0 0; 
0080           0 0 0 1 1 0 1 1 0 0; 
0081           0 0 1 1 0 0 1 1 0 0; 
0082           0 1 1 0 0 0 1 1 0 0; 
0083           0 1 1 0 0 0 1 1 0 0; 
0084           0 1 1 1 1 1 1 1 1 0; 
0085           0 0 0 0 0 0 1 1 0 0; 
0086           0 0 0 0 0 0 1 1 0 0; 
0087           0 0 0 0 0 0 1 1 0 0; 
0088           0 0 0 0 0 0 1 1 0 0; 
0089           0 0 0 0 0 0 0 0 0 0],  
0090        
0091          [0 0 0 0 0 0 0 0 0 0; 
0092           0 1 1 1 1 1 1 1 1 0; 
0093           0 1 1 0 0 0 0 0 0 0; 
0094           0 1 1 0 0 0 0 0 0 0; 
0095           0 1 1 0 0 0 0 0 0 0; 
0096           0 1 1 0 0 0 0 0 0 0; 
0097           0 1 1 0 1 1 1 0 0 0; 
0098           0 1 1 1 0 0 1 1 0 0; 
0099           0 0 0 0 0 0 0 1 1 0; 
0100           0 0 0 0 0 0 0 1 1 0; 
0101           0 0 0 0 0 0 0 1 1 0; 
0102           0 1 1 0 0 0 0 1 1 0; 
0103           0 0 1 1 0 0 1 1 0 0; 
0104           0 0 0 1 1 1 1 0 0 0; 
0105           0 0 0 0 0 0 0 0 0 0],  
0106        
0107          [0 0 0 0 0 0 0 0 0 0; 
0108           0 0 0 1 1 1 1 0 0 0; 
0109           0 0 1 1 0 0 1 1 0 0; 
0110           0 1 1 0 0 0 0 1 0 0; 
0111           0 1 1 0 0 0 0 0 0 0; 
0112           0 1 1 0 0 0 0 0 0 0; 
0113           0 1 1 0 1 1 1 0 0 0; 
0114           0 1 1 1 0 0 1 1 0 0; 
0115           0 1 1 0 0 0 0 1 1 0; 
0116           0 1 1 0 0 0 0 1 1 0; 
0117           0 1 1 0 0 0 0 1 1 0; 
0118           0 1 1 0 0 0 0 1 1 0; 
0119           0 0 1 1 0 0 1 1 0 0; 
0120           0 0 0 1 1 1 1 0 0 0; 
0121           0 0 0 0 0 0 0 0 0 0],  
0122        
0123          [0 0 0 0 0 0 0 0 0 0; 
0124           0 1 1 1 1 1 1 1 1 0; 
0125           0 0 0 0 0 0 0 1 1 0; 
0126           0 0 0 0 0 0 0 1 1 0; 
0127           0 0 0 0 0 0 1 1 0 0; 
0128           0 0 0 0 0 0 1 1 0 0; 
0129           0 0 0 0 0 1 1 0 0 0; 
0130           0 0 0 0 0 1 1 0 0 0; 
0131           0 0 0 0 1 1 0 0 0 0; 
0132           0 0 0 0 1 1 0 0 0 0; 
0133           0 0 0 1 1 0 0 0 0 0; 
0134           0 0 0 1 1 0 0 0 0 0; 
0135           0 0 1 1 0 0 0 0 0 0; 
0136           0 0 1 1 0 0 0 0 0 0; 
0137           0 0 0 0 0 0 0 0 0 0],  
0138          
0139          [0 0 0 0 0 0 0 0 0 0; 
0140           0 0 0 1 1 1 1 0 0 0; 
0141           0 0 1 1 0 0 1 1 0 0; 
0142           0 1 1 0 0 0 0 1 1 0; 
0143           0 1 1 0 0 0 0 1 1 0; 
0144           0 1 1 0 0 0 0 1 1 0; 
0145           0 0 1 1 0 0 1 1 0 0; 
0146           0 0 0 1 1 1 1 0 0 0; 
0147           0 0 1 1 0 0 1 1 0 0; 
0148           0 1 1 0 0 0 0 1 1 0; 
0149           0 1 1 0 0 0 0 1 1 0; 
0150           0 1 1 0 0 0 0 1 1 0; 
0151           0 0 1 1 0 0 1 1 0 0; 
0152           0 0 0 1 1 1 1 0 0 0; 
0153           0 0 0 0 0 0 0 0 0 0],  
0154           
0155          [0 0 0 0 0 0 0 0 0 0; 
0156           0 0 0 1 1 1 1 0 0 0; 
0157           0 0 1 1 0 0 1 1 0 0; 
0158           0 1 1 0 0 0 0 1 1 0; 
0159           0 1 1 0 0 0 0 1 1 0; 
0160           0 1 1 0 0 0 0 1 1 0; 
0161           0 1 1 0 0 0 0 1 1 0; 
0162           0 0 1 1 0 0 1 1 1 0; 
0163           0 0 0 1 1 1 0 1 1 0; 
0164           0 0 0 0 0 0 0 1 1 0; 
0165           0 0 0 0 0 0 0 1 1 0; 
0166           0 0 1 0 0 0 0 1 1 0; 
0167           0 0 1 1 0 0 1 1 0 0; 
0168           0 0 0 1 1 1 1 0 0 0; 
0169           0 0 0 0 0 0 0 0 0 0],  
0170           
0171          [0 0 0 0 0 0 0 0 0 0; 
0172           0 0 0 0 1 1 0 0 0 0; 
0173           0 0 0 1 1 1 1 0 0 0; 
0174           0 0 1 1 0 0 1 1 0 0; 
0175           0 0 1 1 0 0 1 1 0 0; 
0176           0 1 1 0 0 0 0 1 1 0; 
0177           0 1 1 0 0 0 0 1 1 0; 
0178           0 1 1 0 0 0 0 1 1 0; 
0179           0 1 1 0 0 0 0 1 1 0; 
0180           0 1 1 0 0 0 0 1 1 0; 
0181           0 0 1 1 0 0 1 1 0 0; 
0182           0 0 1 1 0 0 1 1 0 0; 
0183           0 0 0 1 1 1 1 0 0 0; 
0184           0 0 0 0 1 1 0 0 0 0; 
0185           0 0 0 0 0 0 0 0 0 0],  
0186          
0187          [0 0 0 0 0 0 0 0 0 0; 
0188           0 0 0 0 0 0 0 0 0 0; 
0189           0 0 0 0 0 0 0 0 0 0; 
0190           0 0 0 0 0 0 0 0 0 0; 
0191           0 0 0 0 0 0 0 0 0 0; 
0192           0 0 0 0 0 0 0 0 0 0; 
0193           0 0 0 0 0 0 0 0 0 0; 
0194           0 0 0 0 0 0 0 0 0 0; 
0195           0 0 0 0 0 0 0 0 0 0; 
0196           0 0 0 0 0 0 0 0 0 0; 
0197           0 0 0 0 0 0 0 0 0 0; 
0198           0 0 0 1 1 1 1 0 0 0; 
0199           0 0 0 1 1 1 1 0 0 0; 
0200           0 0 0 1 1 1 1 0 0 0; 
0201           0 0 0 0 0 0 0 0 0 0],  
0202           
0203          [0 0 0 0 0 0 0 0 0 0; 
0204           0 0 0 0 0 0 0 0 0 0; 
0205           0 0 0 0 0 0 0 0 0 0; 
0206           0 0 0 0 0 0 0 0 0 0; 
0207           0 0 0 0 0 0 0 0 0 0; 
0208           0 0 0 0 0 0 0 0 0 0; 
0209           0 0 0 0 0 0 0 0 0 0; 
0210           0 1 1 1 1 1 1 1 1 0; 
0211           0 0 0 0 0 0 0 0 0 0; 
0212           0 0 0 0 0 0 0 0 0 0; 
0213           0 0 0 0 0 0 0 0 0 0; 
0214           0 0 0 0 0 0 0 0 0 0; 
0215           0 0 0 0 0 0 0 0 0 0; 
0216           0 0 0 0 0 0 0 0 0 0; 
0217           0 0 0 0 0 0 0 0 0 0],  
0218           
0219          [0 0 0 0 0 0 0 0 0 0; 
0220           0 0 0 0 0 0 0 0 0 0; 
0221           0 0 0 0 0 0 0 0 0 0; 
0222           0 0 0 0 0 0 0 0 0 0; 
0223           0 0 0 0 0 0 0 0 0 0; 
0224           0 0 0 1 1 1 1 0 0 0; 
0225           0 0 1 1 0 0 0 1 1 0; 
0226           0 1 1 0 0 0 0 1 1 0; 
0227           0 1 1 1 1 1 1 1 1 0; 
0228           0 1 0 0 1 0 0 0 0 0; 
0229           0 1 1 0 0 0 0 0 0 0; 
0230           0 1 1 0 0 0 0 0 0 0; 
0231           0 0 1 1 0 0 0 1 1 0; 
0232           0 0 0 1 1 1 1 1 0 0; 
0233           0 0 0 0 0 0 0 0 0 0],  
0234                                  }; 
0235  
0236 % Characters defined to identify the numbers
0237 chars = {'1', '2', '3','4', '5', '6', '7','8', '9','0','.','-','e'}; 
0238  
0239 % If color matrix is not defined use default color matrix
0240 if nargin < 2 
0241     c = []; 
0242 end 
0243 if isempty(c)    
0244     c = x/max(x(:))*.9; 
0245 else 
0246     c = c*.9; 
0247 end 
0248  
0249 %If alpha_value is not defined use default value
0250 if nargin < 3 
0251     alpha_value = 1; 
0252 end 
0253  
0254 clf; 
0255  
0256 [numrow,numcol] = size(x); % number of rows and columns of the matrix x
0257 %total_elements = numrow*numcol;
0258 con = 1; % counter to find the maximum size of the texture matrix
0259 for row=1:numrow 
0260    for col=1:numcol, 
0261        %To convert each number to its corresponding texture matrix
0262        num_matrix = number2matrix(x(row,col),chars,bitmapdata); 
0263        p{row,col} = num_matrix; 
0264        [rowdata,coldata] = size(num_matrix);     % number of rows and columns of the texture matrix
0265        max_dim_temp(con) = max(rowdata,coldata); % save the maximum
0266        con = con+1;                              % increment counter con
0267    end % end of "for col"
0268 end % end of "for row"
0269  
0270 max_dim = max(max_dim_temp); % pick the maximum size from all the texture matrices
0271  
0272 for i = 1:numrow 
0273     for k = 1:numcol 
0274          % Assign the texture matrix to variable data starting from last row and first column
0275         data = p{numrow+1-i,k};         
0276         [rowdata,coldata] = size(data); 
0277         factorY = rowdata/max_dim;   % scaling factor according to the dimenions of the texture matrix
0278         factorX = coldata/max_dim;   % scaling factor according to the dimenions of the texture matrix
0279         x_init_back = [k-1,k];       % column index for background box
0280         y_init_back = [i-1,i];       % row index for background box
0281         a1 = x_init_back(1)+0.5-factorX/2;    % x-coor of bottom left corner of the texture
0282         a2 = x_init_back(1)+0.5+factorX/2;    % x-coor of bottom right corner of the texture
0283         
0284         b1 = y_init_back(1)+0.5-factorY/2;    % y-coor of bottom right corner of the texture
0285         b2 = y_init_back(1)+0.5+factorY/2;    % y-coor of upper right corner of the texture
0286         z_init = zeros(length(x_init_back),length(y_init_back)); % zeros matrix to plot surface
0287          
0288         % To plot the background color boxes
0289         back_ground = surface(x_init_back,y_init_back,z_init,c(numrow+1-i,k)); 
0290          
0291         % To plot the foreground box for the texture
0292         for_ground = surface([a1 a2],[b1 b2],z_init); 
0293         set(back_ground,'FaceAlpha',alpha_value)  % to make background transparent
0294          
0295         % To set the texture to the foreground box
0296         set(for_ground,'Cdata',flipud(data),'AlphaData',flipud(data),'FaceColor','Texture',... 
0297                                          'FaceAlpha','Texture','LineStyle','None')  
0298     end 
0299 end 
0300 a = colormap;       % current figure colormap
0301 a(end,:) = [0 0 0]; % to change the last color value to back
0302 colormap(a)         % change the colormap
0303 axis equal          % make axis of the plot equal
0304 
0305 box on              % make border line of the plot visible
0306 axis off            % make the axis numbers and lines invisible
0307 return % end of the main function
0308  
0309 %-----------------------------------------------------------
0310 % Function to convert number (real of intergers) into their corresponding
0311 % texture matrices
0312  
0313 function res = number2matrix(n, chars, bitmapdata) 
0314  
0315 n = num2str(n);                % change number to the string
0316 res = [];                      % initialize res variable
0317 for i = 1:length(n)         
0318     for k = 1:length(chars) 
0319         if n(i) == chars{k} 
0320             m = bitmapdata{k}; % assign texture to the variable
0321             break;             % if number found stop
0322         end % end "if n(i)"
0323     end % end "for k"
0324     res = [res m];             % concatenate the result
0325 end % end "for i"

Generated on Fri 30-Jul-2010 16:56:05 by m2html © 2003