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

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