HOW TO PRODUCE INDEX MATRIX, IDX (K-MEANS CLUSTERING) SAME AS THE INPUT DATA

6 次查看(过去 30 天)
I have this data which is 7125 x 312 matrix.
After conduct a k-means clustering, i want the output of index matrix (IDX value) in 7125 x 312 matrix. After perform the coding, instead of 7125 x 312 matrix, the index matrix that i get is 7125 x 1.
clear all;
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 12;
% Read in data
z = textread('try_reshape.txt');
subplot(3, 1, 3);
[idx,C] = kmeans(z,3,'Distance','cityblock','Replicates',20);
gscatter(z(:,1),z(:,1),idx,'bgm')
hold on
plot(C(:,1),C(:,1),'kx');
grid on;
xlabel('Tidal Range (m)');
ylabel('Tidal Range (m)');
title ('K-mean Clustering of Tidal Range for 23 years data');
legend ('Cluster 1', 'Cluster 2','Cluster 3','Cluster Centroid');
hold off;

回答(1 个)

Walter Roberson
Walter Roberson 2021-12-27
When you call kmeans() then the rows of the matrix are each treated as independent points in space. So your 7125 x 312 matrix is treated as 7125 points in a 312 dimensional space.
Your call is then asking to cluster those 7125 different multi-dimensional points into 3 different clusters.
The result of the kmeans call is going to be a vector of length 7125, with each value being either 1, 2, or 3, reflecting which of the 312-dimensional centroids the row was found to be closest to.
You should not be expecting a 7125 x 312 matrix, because there is only one result per row because each row is part of a multidimensional point.
If what you are trying to do is treat the matrix like a grayscale image, and find 3 grayscale values that best cluster it, then you would have to pass in z(:) to kmeans, and reshape(idx, size(z)) afterwards.
But if that is what you want to do, consider instead using multithresh() https://www.mathworks.com/help/images/ref/multithresh.html

标签

Community Treasure Hunt

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

Start Hunting!

Translated by