-
Notifications
You must be signed in to change notification settings - Fork 1
/
dummyv.m
31 lines (29 loc) · 1.05 KB
/
dummyv.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
function d = dummyv(x, coding)
%DUMMYV (posit) Create a matrix of dummy variables for a grouping var
% D = DUMMYV(X,CODING) creates a dummy variable matrix D from the grouping
% vector X. D will be have rank one less than the number of unique
% grouping variables, (i.e., one column will be omitted). The maximum
% value of the grouping variable will be omitted.
% Each cell will contain either an 0 or a 1.
% It will not include a column of all ones for the mean!
% Group numbers need not be contiguous.
%
% CODING = 'full' (default) means use one column for each variable
% CODING = 'compact' means use the minimum number of columns possible
%
% based on MATLAB DUMMYVAR; bugs removed
%
% MH - http://github.com/histed/tools-mh
if nargin < 2, coding = 'full'; end
switch coding
case 'compact'
error;
case 'full'
[c1,c2,grp]=unique(colvect(x));
d = sparse(1:length(x),grp,1);
d = full(d);
% make it full rank
d = d(:,1:(end-1));
otherwise
error ('Bad coding value');
end