K-means clustering for lower triangle of matrix

2 views (last 30 days)
I am trying to perform k-means clustering on the lower triangle of a 52x52 matrix (m=tril(data,-1)). Basically, I want my final product to look like the graphic attatched below, with the shapes roughly deliniating the boundries of my clusters within my matrix.
I started off by trying to run the code given as an example in the k-means clustering documentation:
X=tril(data,-1);
[idx,C] = kmeans(X,3);
x1 = min(X(:,1)):0.01:max(X(:,1));
x2 = min(X(:,2)):0.01:max(X(:,2));
[x1G,x2G] = meshgrid(x1,x2);
XGrid = [x1G(:),x2G(:)]; % Defines a fine grid on the plot
idx2Region = kmeans(XGrid,3,'MaxIter',1,'Start',C);
However, when I try to run this code I get the error message
"Error using kmeans (line 236)
The 'Start' matrix must have the same number of columns as X."
What do I need to do to fix this? And/or is there a better way of going about this? Thank you so much!

Answers (1)

Walter Roberson
Walter Roberson on 3 Apr 2022
X=tril(data,-1);
That is going to createa a 52 x 52 matix
[idx,C] = kmeans(X,3);
Each row of the matrix will be treated as the coordinates of a point. The output will be 52 rows of indices in idx (number of rows in X), and C will be a 3 x 52 matrix (number of clusters by number of columns in X)
XGrid = [x1G(:),x2G(:)]; % Defines a fine grid on the plot
That will only have two columns.

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!