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