Code covered by the BSD License  

Highlights from
Indicator matrix generation from survey questions.

from Indicator matrix generation from survey questions. by Antonio Trujillo-Ortiz
indicator matrix, multiple correspondence analysis, crosstabulation analysis, categorical analysis.

x=indmatgen(lcv,table)
function x=indmatgen(lcv,table)
%INDMATGEN Indicator matrix generation from survey questions.
% An indicator matrix (G), a binary coding matrix of the factors called
% dummy variables. The number of rows are the total sample items and the
% columns are the total categories of the variables. The elements in G are
% 1's if the item corresponding to the category of the variable or 0's if
% not. The indicator matrix is an alternative format to input data in a 
% Multiple Correspondence Analysis from survey questions. Its generation is
% the first step in the procedure of a Multiple Correspondence Analysis 
% (MCA). You can find the MCA based on the indicator or Burt matrix in this
% FEX author's page. The Burt matrix can obtained as B = G'*G.
%
%
% Syntax: function x=indmatgen 
%      
% Input:
%    - lcv: maximum number of levels on each catagorical variable
%    - table: table of data
%              
% Output:
%    - Indicator matrix
%
% Example. Suppose we have done a ten survey questionaires with three 
% categorical variables: Sex (male,female), Marital status (married,widowed,
% single,divorced), and Scholarity (non,elementary school,high school,
% bachelor,university,post-graduate)
%
%  ---------------------------------------------------
%     Sex     Marital status        Scholarity
%  ---------------------------------------------------
%    M   F    M   W   S   D    N   E   H   B   U   P  
%  ---------------------------------------------------
%    X                X            X 
%        X    X                X
%        X            X                    X
%        X            X                    X
%    X            X                            X
%        X                X                        X
%        X    X                    X
%        X    X                            X
%    X                X                        X
%    X        X                                X
%  ---------------------------------------------------
%
%  lcv = [2 4 6]
%  table = [1 3 2;2 1 1;2 3 4;2 3 4;1 2 5;2 4 6;2 1 2;2 1 4;1 3 5;1 1 5]
%
%  Calling on Matlab the function: 
%             X=indmatgen(lcv,table)
%
%  Answer is:
%
%
%  X =
%       1     0     0     0     1     0     0     1     0     0     0     0
%       0     1     1     0     0     0     1     0     0     0     0     0
%       0     1     0     0     1     0     0     0     0     1     0     0
%       0     1     0     0     1     0     0     0     0     1     0     0
%       1     0     0     1     0     0     0     0     0     0     1     0
%       0     1     0     0     0     1     0     0     0     0     0     1
%       0     1     1     0     0     0     0     1     0     0     0     0
%       0     1     1     0     0     0     0     0     0     1     0     0
%       1     0     0     0     1     0     0     0     0     0     1     0
%       1     0     1     0     0     0     0     0     0     0     1     0
%
%  Created by A. Trujillo-Ortiz, R. Hernandez-Walls and K. Barba-Rojo
%             Facultad de Ciencias Marinas
%             Universidad Autonoma de Baja California
%             Apdo. Postal 453
%             Ensenada, Baja California
%             Mexico.
%             atrujo@uabc.mx
%  Copyright. December 30, 2008.
%
%  We improved this m-file by the valuable comment of Jos (05/01/2009).
%
%  To cite this file, this would be an appropriate format:
%  Trujillo-Ortiz, A., R. Hernandez-Walls and K. Barba-Rojo. (2008). indmatgen:
%    Indicator matrix generation from survey questions. A MATLAB file. [WWW document].
%    URL http://mathworks.com/matlabcentral/fileexchange/22560
%

%lcv = input('Give me the maximum number of levels on each catagorical variable as []:');
nA = length(lcv);
%table = input('Give me the table of data as []:');
[nT,mT] = size(table);
for k = 1:mT
    if (max(table(:,k))>lcv(k))
        disp('Error on the data capture. Check it, please.')
    end
end
x = [];
for k = 1:nT
    a = [];
    for r = 1:nA
     tempA = zeros(1,lcv(r));
     tempA(table(k,r)) = 1;
     a = [a,tempA];
    end
    x = [x;a];
end
%x = b;

return,

Contact us at files@mathworks.com