Daniel@0: function [nos,names] = som_label2num(L) Daniel@0: Daniel@0: %SOM_LABEL2NUM Recodes textual data labels to interger class labels Daniel@0: % Daniel@0: % [class,names]=class2num(L) Daniel@0: % Daniel@0: % [class,names]=class2num(sData) Daniel@0: % [class,names]=class2num(sMap) Daniel@0: % [class,names]=class2num(sData.labels); Daniel@0: % Daniel@0: % Input and output arguments ([]'s are optional): Daniel@0: % Daniel@0: % L (map struct, data struct, Daniel@0: % Nx1 cell array of strings, Daniel@0: % a Nxn char array) textual labels Daniel@0: % class (vector) Nx1 vector of integers where N is the number of original text labels Daniel@0: % names (cell) kx1 array of strings where names(i) correspons to integer label i Daniel@0: % Daniel@0: % See also KNN Daniel@0: Daniel@0: % Contributed to SOM Toolbox 2.0, October 29th, 2000 by Johan Himberg Daniel@0: % Copyright (c) by Johan Himberg Daniel@0: % http://www.cis.hut.fi/projects/somtoolbox/ Daniel@0: Daniel@0: % Version 2.0beta Johan 291000 Daniel@0: Daniel@0: %% Init %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: Daniel@0: if isstruct(L); Daniel@0: if isfield(L,'type') & ischar(L.type), Daniel@0: ; Daniel@0: else Daniel@0: error('Invalid map/data struct?'); Daniel@0: end Daniel@0: switch L.type Daniel@0: case {'som_map', 'som_data'} Daniel@0: class=L.labels(:,1); Daniel@0: otherwise error('Invalid map/data struct?'); Daniel@0: end Daniel@0: elseif vis_valuetype(L,{'cellcolumn_of_char'}), Daniel@0: class=L; Daniel@0: elseif vis_valuetype(L,{'chararray'}), Daniel@0: class=cellstr(L); Daniel@0: else Daniel@0: error('Input must be an Nx1 cell array of strings, a char array, a map struct or a data struct.'); Daniel@0: end Daniel@0: Daniel@0: names = {}; Daniel@0: nos = zeros(length(class),1); Daniel@0: for i=1:length(class), Daniel@0: if ~isempty(class{i}) & ~any(strcmp(class{i},names)), Daniel@0: names=cat(1,names,class(i)); Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: tmp_nos = (1:length(names))'; Daniel@0: for i=1:length(class), Daniel@0: if ~isempty(class{i}), Daniel@0: nos(i,1) = find(strcmp(class{i},names)); Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: if any(nos==0), Daniel@0: nos=nos+1; Daniel@0: names(2:end+1)=names; Daniel@0: names{1}=''; Daniel@0: end Daniel@0: Daniel@0: Daniel@0: