Daniel@0: function sMap = som_randinit(D, varargin) Daniel@0: Daniel@0: %SOM_RANDINIT Initialize a Self-Organizing Map with random values. Daniel@0: % Daniel@0: % sMap = som_randinit(D, [[argID,] value, ...]) Daniel@0: % Daniel@0: % sMap = som_randinit(D); Daniel@0: % sMap = som_randinit(D,sMap); Daniel@0: % sMap = som_randinit(D,'munits',100,'hexa'); Daniel@0: % Daniel@0: % Input and output arguments ([]'s are optional): Daniel@0: % D The training data. Daniel@0: % (struct) data struct Daniel@0: % (matrix) data matrix, size dlen x dim Daniel@0: % [argID, (string) Parameters affecting the map topology are given Daniel@0: % value] (varies) as argument ID - argument value pairs, listed below. Daniel@0: % Daniel@0: % sMap (struct) map struct Daniel@0: % Daniel@0: % Here are the valid argument IDs and corresponding values. The values Daniel@0: % which are unambiguous (marked with '*') can be given without the Daniel@0: % preceeding argID. Daniel@0: % 'munits' (scalar) number of map units Daniel@0: % 'msize' (vector) map size Daniel@0: % 'lattice' *(string) map lattice: 'hexa' or 'rect' Daniel@0: % 'shape' *(string) map shape: 'sheet', 'cyl' or 'toroid' Daniel@0: % 'topol' *(struct) topology struct Daniel@0: % 'som_topol','sTopol' = 'topol' Daniel@0: % 'map' *(struct) map struct Daniel@0: % 'som_map','sMap' = 'map' Daniel@0: % Daniel@0: % For more help, try 'type som_randinit' or check out online documentation. Daniel@0: % See also SOM_MAP_STRUCT, SOM_LININIT, SOM_MAKE. Daniel@0: Daniel@0: %%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: % Daniel@0: % som_randinit Daniel@0: % Daniel@0: % PURPOSE Daniel@0: % Daniel@0: % Initializes a SOM with random values. Daniel@0: % Daniel@0: % SYNTAX Daniel@0: % Daniel@0: % sMap = som_randinit(D) Daniel@0: % sMap = som_randinit(D,sMap); Daniel@0: % sMap = som_randinit(D,'munits',100,'hexa'); Daniel@0: % Daniel@0: % DESCRIPTION Daniel@0: % Daniel@0: % Initializes a SOM with random values. If necessary, a map struct Daniel@0: % is created first. For each component (xi), the values are uniformly Daniel@0: % distributed in the range of [min(xi) max(xi)]. Daniel@0: % Daniel@0: % REQUIRED INPUT ARGUMENTS Daniel@0: % Daniel@0: % D The training data. Daniel@0: % (struct) Data struct. If this is given, its '.comp_names' and Daniel@0: % '.comp_norm' fields are copied to the map struct. Daniel@0: % (matrix) data matrix, size dlen x dim Daniel@0: % Daniel@0: % OPTIONAL INPUT ARGUMENTS Daniel@0: % Daniel@0: % argID (string) Argument identifier string (see below). Daniel@0: % value (varies) Value for the argument (see below). Daniel@0: % Daniel@0: % The optional arguments can be given as 'argID',value -pairs. If an Daniel@0: % argument is given value multiple times, the last one is used. Daniel@0: % Daniel@0: % Here are the valid argument IDs and corresponding values. The values Daniel@0: % which are unambiguous (marked with '*') can be given without the Daniel@0: % preceeding argID. Daniel@0: % 'dlen' (scalar) length of the training data Daniel@0: % 'data' (matrix) the training data Daniel@0: % *(struct) the training data Daniel@0: % 'munits' (scalar) number of map units Daniel@0: % 'msize' (vector) map size Daniel@0: % 'lattice' *(string) map lattice: 'hexa' or 'rect' Daniel@0: % 'shape' *(string) map shape: 'sheet', 'cyl' or 'toroid' Daniel@0: % 'topol' *(struct) topology struct Daniel@0: % 'som_topol','sTopol' = 'topol' Daniel@0: % 'map' *(struct) map struct Daniel@0: % 'som_map','sMap' = 'map' Daniel@0: % Daniel@0: % OUTPUT ARGUMENTS Daniel@0: % Daniel@0: % sMap (struct) The initialized map struct. Daniel@0: % Daniel@0: % EXAMPLES Daniel@0: % Daniel@0: % sMap = som_randinit(D); Daniel@0: % sMap = som_randinit(D,sMap); Daniel@0: % sMap = som_randinit(D,sTopol); Daniel@0: % sMap = som_randinit(D,'msize',[10 10]); Daniel@0: % sMap = som_randinit(D,'munits',100,'hexa'); Daniel@0: % Daniel@0: % SEE ALSO Daniel@0: % Daniel@0: % som_map_struct Create a map struct. Daniel@0: % som_lininit Initialize a map using linear initialization algorithm. Daniel@0: % som_make Initialize and train self-organizing map. Daniel@0: Daniel@0: % Copyright (c) 1997-2000 by the SOM toolbox programming team. Daniel@0: % http://www.cis.hut.fi/projects/somtoolbox/ Daniel@0: Daniel@0: % Version 1.0beta ecco 100997 Daniel@0: % Version 2.0beta juuso 101199 Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: %% check arguments Daniel@0: Daniel@0: % data Daniel@0: if isstruct(D), Daniel@0: data_name = D.name; Daniel@0: comp_names = D.comp_names; Daniel@0: comp_norm = D.comp_norm; Daniel@0: D = D.data; Daniel@0: struct_mode = 1; Daniel@0: else Daniel@0: data_name = inputname(1); Daniel@0: struct_mode = 0; Daniel@0: end Daniel@0: [dlen dim] = size(D); Daniel@0: Daniel@0: % varargin Daniel@0: sMap = []; Daniel@0: sTopol = som_topol_struct; Daniel@0: sTopol.msize = 0; Daniel@0: munits = NaN; Daniel@0: i=1; Daniel@0: while i<=length(varargin), Daniel@0: argok = 1; Daniel@0: if ischar(varargin{i}), Daniel@0: switch varargin{i}, Daniel@0: case 'munits', i=i+1; munits = varargin{i}; sTopol.msize = 0; Daniel@0: case 'msize', i=i+1; sTopol.msize = varargin{i}; Daniel@0: munits = prod(sTopol.msize); Daniel@0: case 'lattice', i=i+1; sTopol.lattice = varargin{i}; Daniel@0: case 'shape', i=i+1; sTopol.shape = varargin{i}; Daniel@0: case {'som_topol','sTopol','topol'}, i=i+1; sTopol = varargin{i}; Daniel@0: case {'som_map','sMap','map'}, i=i+1; sMap = varargin{i}; sTopol = sMap.topol; Daniel@0: case {'hexa','rect'}, sTopol.lattice = varargin{i}; Daniel@0: case {'sheet','cyl','toroid'}, sTopol.shape = varargin{i}; Daniel@0: otherwise argok=0; Daniel@0: end Daniel@0: elseif isstruct(varargin{i}) & isfield(varargin{i},'type'), Daniel@0: switch varargin{i}.type, Daniel@0: case 'som_topol', Daniel@0: sTopol = varargin{i}; Daniel@0: case 'som_map', Daniel@0: sMap = varargin{i}; Daniel@0: sTopol = sMap.topol; Daniel@0: otherwise argok=0; Daniel@0: end Daniel@0: else Daniel@0: argok = 0; Daniel@0: end Daniel@0: if ~argok, Daniel@0: disp(['(som_topol_struct) Ignoring invalid argument #' num2str(i)]); Daniel@0: end Daniel@0: i = i+1; Daniel@0: end Daniel@0: Daniel@0: if ~isempty(sMap), Daniel@0: [munits dim2] = size(sMap.codebook); Daniel@0: if dim2 ~= dim, error('Map and data must have the same dimension.'); end Daniel@0: end Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: %% create map Daniel@0: Daniel@0: % map struct Daniel@0: if ~isempty(sMap), Daniel@0: sMap = som_set(sMap,'topol',sTopol); Daniel@0: else Daniel@0: if ~prod(sTopol.msize), Daniel@0: if isnan(munits), Daniel@0: sTopol = som_topol_struct('data',D,sTopol); Daniel@0: else Daniel@0: sTopol = som_topol_struct('data',D,'munits',munits,sTopol); Daniel@0: end Daniel@0: end Daniel@0: sMap = som_map_struct(dim, sTopol); Daniel@0: end Daniel@0: Daniel@0: if struct_mode, Daniel@0: sMap = som_set(sMap,'comp_names',comp_names,'comp_norm',comp_norm); Daniel@0: end Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: %% initialization Daniel@0: Daniel@0: % train struct Daniel@0: sTrain = som_train_struct('algorithm','randinit'); Daniel@0: sTrain = som_set(sTrain,'data_name',data_name); Daniel@0: Daniel@0: munits = prod(sMap.topol.msize); Daniel@0: sMap.codebook = rand([munits dim]); Daniel@0: Daniel@0: % set interval of each component to correct value Daniel@0: for i = 1:dim, Daniel@0: inds = find(~isnan(D(:,i)) & ~isinf(D(:,i))); Daniel@0: if isempty(inds), mi = 0; ma = 1; Daniel@0: else ma = max(D(inds,i)); mi = min(D(inds,i)); Daniel@0: end Daniel@0: sMap.codebook(:,i) = (ma - mi) * sMap.codebook(:,i) + mi; Daniel@0: end Daniel@0: Daniel@0: % training struct Daniel@0: sTrain = som_set(sTrain,'time',datestr(now,0)); Daniel@0: sMap.trainhist = sTrain; Daniel@0: Daniel@0: return; Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%