annotate toolboxes/MIRtoolbox1.3.2/somtoolbox/som_label2num.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 function [nos,names] = som_label2num(L)
wolffd@0 2
wolffd@0 3 %SOM_LABEL2NUM Recodes textual data labels to interger class labels
wolffd@0 4 %
wolffd@0 5 % [class,names]=class2num(L)
wolffd@0 6 %
wolffd@0 7 % [class,names]=class2num(sData)
wolffd@0 8 % [class,names]=class2num(sMap)
wolffd@0 9 % [class,names]=class2num(sData.labels);
wolffd@0 10 %
wolffd@0 11 % Input and output arguments ([]'s are optional):
wolffd@0 12 %
wolffd@0 13 % L (map struct, data struct,
wolffd@0 14 % Nx1 cell array of strings,
wolffd@0 15 % a Nxn char array) textual labels
wolffd@0 16 % class (vector) Nx1 vector of integers where N is the number of original text labels
wolffd@0 17 % names (cell) kx1 array of strings where names(i) correspons to integer label i
wolffd@0 18 %
wolffd@0 19 % See also KNN
wolffd@0 20
wolffd@0 21 % Contributed to SOM Toolbox 2.0, October 29th, 2000 by Johan Himberg
wolffd@0 22 % Copyright (c) by Johan Himberg
wolffd@0 23 % http://www.cis.hut.fi/projects/somtoolbox/
wolffd@0 24
wolffd@0 25 % Version 2.0beta Johan 291000
wolffd@0 26
wolffd@0 27 %% Init %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wolffd@0 28
wolffd@0 29 if isstruct(L);
wolffd@0 30 if isfield(L,'type') & ischar(L.type),
wolffd@0 31 ;
wolffd@0 32 else
wolffd@0 33 error('Invalid map/data struct?');
wolffd@0 34 end
wolffd@0 35 switch L.type
wolffd@0 36 case {'som_map', 'som_data'}
wolffd@0 37 class=L.labels(:,1);
wolffd@0 38 otherwise error('Invalid map/data struct?');
wolffd@0 39 end
wolffd@0 40 elseif vis_valuetype(L,{'cellcolumn_of_char'}),
wolffd@0 41 class=L;
wolffd@0 42 elseif vis_valuetype(L,{'chararray'}),
wolffd@0 43 class=cellstr(L);
wolffd@0 44 else
wolffd@0 45 error('Input must be an Nx1 cell array of strings, a char array, a map struct or a data struct.');
wolffd@0 46 end
wolffd@0 47
wolffd@0 48 names = {};
wolffd@0 49 nos = zeros(length(class),1);
wolffd@0 50 for i=1:length(class),
wolffd@0 51 if ~isempty(class{i}) & ~any(strcmp(class{i},names)),
wolffd@0 52 names=cat(1,names,class(i));
wolffd@0 53 end
wolffd@0 54 end
wolffd@0 55
wolffd@0 56 tmp_nos = (1:length(names))';
wolffd@0 57 for i=1:length(class),
wolffd@0 58 if ~isempty(class{i}),
wolffd@0 59 nos(i,1) = find(strcmp(class{i},names));
wolffd@0 60 end
wolffd@0 61 end
wolffd@0 62
wolffd@0 63 if any(nos==0),
wolffd@0 64 nos=nos+1;
wolffd@0 65 names(2:end+1)=names;
wolffd@0 66 names{1}='';
wolffd@0 67 end
wolffd@0 68
wolffd@0 69
wolffd@0 70