MATLAB Self-Organizing Map SOM clustering
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
A self-organizing map, or SOM, is an unsupervised neural network that projects high-dimensional data onto a small grid while preserving neighborhood structure as much as possible. In MATLAB, SOMs are most useful when you want clustering and visualization together: similar observations activate nearby neurons, so the map becomes both a grouping tool and a diagnostic view.
What a SOM Is Actually Doing
A SOM does not assign clusters directly in the same way that kmeans does. Instead, it learns a grid of prototype vectors called codebook vectors. For each input sample, the map finds the best matching unit, or BMU, then nudges that neuron and its neighbors toward the sample.
That neighborhood update is what gives the map its topology. Nearby neurons on the 2D grid learn to represent similar regions of the input space. Once training is complete, you can use the BMU index or the distances between prototype vectors to derive clusters.
Train a SOM in MATLAB
MATLAB provides the selforgmap function in the Deep Learning Toolbox. The input matrix must be arranged with columns as observations and rows as features, which is a common source of mistakes.
The fisheriris dataset is a simple example because it has four numeric features and known species labels.
What this code does:
- standardizes the features so one measurement does not dominate the rest
- creates a
4 x 4map with 16 neurons - trains the SOM on the input columns
- converts the one-hot neuron activations into BMU indices
- plots hit counts so you can see how samples distribute across the map
The plot does not magically label clusters for you. It shows where samples land, which is the starting point for interpreting structure.
Turn the Map Into Clusters
A common workflow is to cluster the prototype vectors after training. In MATLAB, the codebook vectors for the map live in net.IW{1}.
Here the SOM first reduces the problem to 16 learned prototypes, and kmeans then groups those prototypes into 3 clusters. Each sample inherits the cluster label of its BMU.
That two-stage approach is often easier to interpret than running a SOM and pretending the map size itself is the number of clusters.
Visualize the Result
A SOM is most useful when you inspect the learned structure. MATLAB includes several visualization helpers.
These figures answer different questions:
- '
plotsomhitsshows which neurons attract many samples.' - '
plotsomndhighlights distances between neighboring neurons and can suggest cluster boundaries.' - '
plotsomplanesshows how each feature varies across the grid.'
If neighboring cells have large distances in the neighbor-distance plot, you are likely seeing a separation between regions of the data space.
Choose the Map Size Carefully
A very small map can collapse distinct groups together. A very large map can overfit noise and make interpretation harder. There is no universal best size, but a moderate grid such as 4 x 4 or 6 x 6 is often a reasonable starting point for small datasets.
The most important preprocessing step is scaling. Since SOM training depends on distance, features with large numeric ranges dominate unless you normalize them first. zscore is a sensible default for many tabular problems.
Common Pitfalls
- Passing data in the wrong orientation. For
selforgmap, columns are samples and rows are features. - Training on unscaled features, which distorts the distance calculations.
- Assuming each SOM neuron is already a final business-level cluster.
- Picking a map that is so large it becomes sparse and hard to interpret.
- Judging the model only by the hit plot without looking at neighbor distances or component planes.
Summary
- A SOM learns a grid of prototype vectors, not final labels by itself.
- In MATLAB, use
selforgmapand arrange the input as features by samples. - Standardize features before training because SOMs are distance-based.
- Use BMU indices and, if needed, cluster the codebook vectors to get cluster labels.
- Inspect hit counts, neighbor distances, and component planes to understand the map.

