How to cluster a dataset having a vector of clustered indeces?

2 ビュー (過去 30 日間)
Madina Makhmutova
Madina Makhmutova 2019 年 4 月 11 日
コメント済み: Adam Danz 2019 年 4 月 15 日
Hello,
My question is very primitive, I'm trying to cluster my dataset using k-means and plot clastered data in heatmap.
I've tried to write the following primitive code but I don't understand why it is not working. Could you please help me figure out why my code is not working and what would be the shortest way to do this simple clustering?
Thank you!
n = 3; % specify the number of clusters that you want the dataset to be divided into
data = randi(100,10,9);
clust_idx = kmeans(data,n);
clustered = [];
for j = 1:n
for i = 1:length(clust_idx)
if clust_idx(i)==j, clustered = [clustered, (data(:,i))];
end
end
end
f = figure(1);
fh = heatmap((clustered)','XLabel','Time(min)','YLabel','Cell #','Colormap',jet);
  2 件のコメント
Adam Danz
Adam Danz 2019 年 4 月 11 日
1) What part of the code isn't working?
2) What does it mean that the code isn't working? Are you getting an error message (if yes, share the entire message)?
The first 3 lines of your code should workfine as long as you're working with decent data. What are the loops for?
Madina Makhmutova
Madina Makhmutova 2019 年 4 月 11 日
Hello Adam,
Thank you for your responce.
I realized that this code is only working when the data table is symetric (10x10 like in the example above), however if the data table is not symmetric ex: randi(100, 10, 9) than I get an error 'index exceeds matrix dimentions' .
The loops are to make a new matrix of data that is clustered according to the k-means clustering. So when i use:
clust_idx = kmeans(data,n)
this line of code gives a vector of cluster indices. So what I have is the vector of clustered indeces and what I want is the clustered data table. Pretty simple task, but I don't know how to get it done.

サインインしてコメントする。

採用された回答

Adam Danz
Adam Danz 2019 年 4 月 11 日
編集済み: Adam Danz 2019 年 4 月 15 日
Your data is a matrix of size [10 x 9].
kmeans() identifies the cluster of each row of the matrix so its output will be a vector whose length is equal to the number of rows of your matrix (10 rows).
Your i-loop loops through each row (1:10). But your indexing your data by column: data(:,1). You only have 9 columns so on the last iteration, there's an error.
I think what you meant to write is:
clustered = [clustered; (data(i,:))];
% ^ ^ Note the restructuring.
Now your code works for the [10 x 9] inputs.
  2 件のコメント
Madina Makhmutova
Madina Makhmutova 2019 年 4 月 15 日
Thank you Adam,
Your explanation helped me to see my mistake, what I needed to do was to cluster along columns not the raws so just transposing the data for the initial clustering did the trick.
clust_idx = kmeans(data',n)
% ^ this little sign made the difference
Adam Danz
Adam Danz 2019 年 4 月 15 日
Nice! Glad I could (indirectly) help.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCluster Analysis and Anomaly Detection についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by