Daniel@0: function net = som(nin, map_size) Daniel@0: %SOM Creates a Self-Organising Map. Daniel@0: % Daniel@0: % Description Daniel@0: % NET = SOM(NIN, MAP_SIZE) creates a SOM NET with input dimension (i.e. Daniel@0: % data dimension) NIN and map dimensions MAP_SIZE. Only two- Daniel@0: % dimensional maps are currently implemented. Daniel@0: % Daniel@0: % The fields in NET are Daniel@0: % type = 'som' Daniel@0: % nin = number of inputs Daniel@0: % map_dim = dimension of map (constrained to be 2) Daniel@0: % map_size = grid size: number of nodes in each dimension Daniel@0: % num_nodes = number of nodes: the product of values in map_size Daniel@0: % map = map_dim+1 dimensional array containing nodes Daniel@0: % inode_dist = map of inter-node distances using Manhatten metric Daniel@0: % Daniel@0: % The map contains the node vectors arranged column-wise in the first Daniel@0: % dimension of the array. Daniel@0: % Daniel@0: % See also Daniel@0: % KMEANS, SOMFWD, SOMTRAIN Daniel@0: % Daniel@0: Daniel@0: % Copyright (c) Ian T Nabney (1996-2001) Daniel@0: Daniel@0: net.type = 'som'; Daniel@0: net.nin = nin; Daniel@0: Daniel@0: % Create Map of nodes Daniel@0: if round(map_size) ~= map_size | (map_size < 1) Daniel@0: error('SOM specification must contain positive integers'); Daniel@0: end Daniel@0: Daniel@0: net.map_dim = length(map_size); Daniel@0: if net.map_dim ~= 2 Daniel@0: error('SOM is a 2 dimensional map'); Daniel@0: end Daniel@0: net.num_nodes = prod(map_size); Daniel@0: % Centres are stored by column as first index of multi-dimensional array. Daniel@0: % This makes extracting them later more easy. Daniel@0: % Initialise with rand to create square grid Daniel@0: net.map = rand([nin, map_size]); Daniel@0: net.map_size = map_size; Daniel@0: Daniel@0: % Crude function to compute inter-node distances Daniel@0: net.inode_dist = zeros([map_size, net.num_nodes]); Daniel@0: for m = 1:net.num_nodes Daniel@0: node_loc = [1+fix((m-1)/map_size(2)), 1+rem((m-1),map_size(2))]; Daniel@0: for k = 1:map_size(1) Daniel@0: for l = 1:map_size(2) Daniel@0: net.inode_dist(k, l, m) = round(max(abs([k l] - node_loc))); Daniel@0: end Daniel@0: end Daniel@0: end