Home > utils > make_mask_mtx.m

make_mask_mtx

PURPOSE ^

[mask_mtx, unique_vals] = make_mask_mtx(data_vect);

SYNOPSIS ^

function [mask_mtx, unique_vals] = make_mask_mtx(data_vect)

DESCRIPTION ^

 [mask_mtx, unique_vals] = make_mask_mtx(data_vect);

 Given a data vector (data_vect) containing some set of values or identifiers, a
 matrix (mask_mtx) and list of unique values (unique_vals) are returned in which
 each column of mask_mtx contains a logical value indicating the locations within data_vect
 that correspond to a unique values in unique_vals.  The list of unique
 valuess is returned as dered by unique.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [mask_mtx, unique_vals] = make_mask_mtx(data_vect)
0002 % [mask_mtx, unique_vals] = make_mask_mtx(data_vect);
0003 %
0004 % Given a data vector (data_vect) containing some set of values or identifiers, a
0005 % matrix (mask_mtx) and list of unique values (unique_vals) are returned in which
0006 % each column of mask_mtx contains a logical value indicating the locations within data_vect
0007 % that correspond to a unique values in unique_vals.  The list of unique
0008 % valuess is returned as dered by unique.
0009 %
0010 
0011 % 02/14/07 Petr Janata - generalized from ensemble_make_sub_masks
0012 % 10May2013 PJ - optimized by using == and strcmp instead of ismember
0013 
0014 if size(data_vect,2) > 1
0015     nrows = size(data_vect,1);
0016     if iscell(data_vect)
0017         tmpdata = data_vect;
0018         nvars = size(tmpdata,2);
0019         numCombos = 0;
0020         while size(tmpdata,1)
0021             numCombos = numCombos+1;
0022             if mod(numCombos,20) == 0
0023                 fprintf('%d', numCombos);
0024             else
0025                 fprintf('.');
0026             end
0027             
0028             % Get a combination
0029             currCombo = tmpdata(1,:);
0030             
0031             % Store it for future use
0032             [unique_vals(numCombos,1:nvars)] = deal(currCombo);
0033             
0034             mask_mtx(:,numCombos) = all(strcmp(data_vect,repmat(currCombo,nrows,1)),2);
0035                         
0036             currComboMask = all(strcmp(tmpdata,repmat(currCombo,size(tmpdata,1),1)),2);
0037             % Eliminate those rows
0038             tmpdata(currComboMask,:) = [];
0039         end
0040         
0041         fprintf('\n');
0042         return
0043     else
0044         unique_vals = unique(data_vect,'rows');
0045     end
0046 else
0047     unique_vals = unique(data_vect);
0048 end
0049 
0050 % Eliminate NaNs if the exist
0051 if isnumeric(unique_vals)
0052   unique_vals(isnan(unique_vals)) = [];
0053 end
0054 
0055 nvals = length(unique_vals);
0056 mask_mtx = false(size(data_vect,1),nvals);
0057 
0058 fprintf('Calculating masks for %d unique values\n', nvals);
0059 for ival = 1:nvals
0060     if mod(ival,20) == 0
0061         fprintf('%d', ival);
0062     else
0063         fprintf('.');
0064   end
0065   if isnumeric(unique_vals)
0066     mask_mtx(:,ival) = data_vect == unique_vals(ival);
0067   elseif iscell(unique_vals(ival)) && ischar(unique_vals{ival})
0068     mask_mtx(:,ival) = strcmp(unique_vals{ival}, data_vect);
0069   else
0070     mask_mtx(:,ival) = ismember(data_vect, unique_vals(ival));
0071   end
0072 end
0073 fprintf('\n');
0074 
0075 return
0076

Generated on Wed 20-Sep-2023 04:00:50 by m2html © 2003