File Exchange

image thumbnail

Self-Organizing Map - Simple demonstration

version (2.68 KB) by George Azzopardi
A simple and neat implementation of a self-organizing map algorithm


Updated 21 Jan 2013

View License

While there are many sources that provide the pseudo-code of a self-organizing map (SOM), I could not find a simple implementation that clarifies every step of this algorithm which was introduced by Kohonen.

For this reason I implemented this clearly defined Matlab implementation and wanted to share it with you.

As a demonstration I use the RGB 3D training vectors. The trained SOM neurons result in a 2D spatial arrangement such that the neurons that have similar 3D weight vectors (i.e. similar colour representation) are located close to each other.

Cite As

George Azzopardi (2021). Self-Organizing Map - Simple demonstration (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (11)


I want to apply it in my datasets with rows=289 and ncol=30
instead of generating a training sample randomly. How to modify the code accordingly

Elina Montero

George Azzopardi

I guess the problem is with the imagesc function. It expects the values to be between 0 and 1. You can try to divide with the maximum value before calling the imagesc function.


I have question : if I want to feed the SOM with my input and the input are integer.

% Generate random training data
trainingData = rand(ntrainingvectors,nfeatures) * 10 ; <----- int values

it does not accept that and complain.

Error using image
TrueColor CData contains element out of range 0.0 <= value <= 1.0

Error in imagesc (line 19)
hh = image(varargin{1},'CDataMapping','scaled');

Error in SOMSimple>displaySOMmap (line 127)

Error in SOMSimple (line 100)
displaySOMmap(fig, 2, ['Epoch: ',num2str(t),'/',num2str(nepochs),', Training
Vector: ',num2str(ntraining),'/',num2str(ntrainingvectors)], som, nfeatures);

Error in SOMSimpleMain (line 36)
what is the solution?

ben harrison

I'm probably missing something, but the SOM Toolbox (a MATLAB library under the GNU GPL) was written by Kohonen's colleagues and students, and contains what might be described as the 'official' implementation of the algorithm, many visualisations, etc.

The 2000 paper/tutorial by Juha Vesanto also provides a detailed description of the implementation (Vesanto, 2000. Neural network tool for data mining: SOM Toolbox, same website as above).

George Azzopardi

Could you write the Matlab command that you are using to call the SOMSImple function?

I suggest that you read the article called "Self-Organizing Map-Based Color Image Segmentation with k-Means Clustering and Saliency Map" which you can find in


I have this 'rather' big RGB image of 960 x 1280 pixels. Actually I have trouble preparing the image for SOM, as few attempts I tried on different SOM codes, it has error of 'out of memory' which I assume either the image has too much data for SOM to process or I prepared it wrongly. The purpose I want to try SOM is for image segmentation purpose.

George Azzopardi

Hi Lim, could you kindly elaborate further on your question? An SOM is a unsupervised clustering method which can be used for dimensionality reduction. In thsi simple example I am initializing a matrix of 3D neurons such that I can show them as RGB values. I wish you can be more specific such that I can help you better.

George Azzopardi

Oops, I wanted to write: "... elaborate further on your question?"


Hi. May I know how can I test this code on my own image please?

Afshin Abbasi

MATLAB Release Compatibility
Created with R2009b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!