Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
k-means

Subject: k-means

From: Fredro

Date: 16 Sep, 2011 04:25:29

Message: 1 of 3

Hi,
I am trying to use k-means in order to get the euclidean distance for each point to the cluster centroid location.
I did the followings:
M=dlmread('Matrix.txt',' ') --> Here is where I load all my data to be clustered
[IDX,C] = kmeans(M,10) --> Getting my centroid location in C with k=10
X=dlmread('Data.txt',' ') --> Where I load one part of my data

Then, I run the following:
[IDX,C,sumd,D] = kmeans(X,k) --> to get the distances in D
but it will give an error message as I need to supply a value for k.
If I do:
[IDX,C,sumd,D] = kmeans(X,10)
then my C (or the centroid location) will change.

How can I preserve my cluster centroid location and obtain the distances for each point of X in this case?

Thank you. I look forward to your clarification.

Kind Regards,

Fred

Subject: k-means

From: Tom Lane

Date: 16 Sep, 2011 19:51:19

Message: 2 of 3

> [IDX,C] = kmeans(M,10) --> Getting my centroid location in C with k=10
...
> [IDX,C,sumd,D] = kmeans(X,10)
> then my C (or the centroid location) will change.

Fred, it sounds like you want to compute C from M, then just assign points
in X to one of the C rows based on the distance -- not do any re-clustering.
I think you just want to compute the distances and find the minimum
directly. Try this:

x = [rand(5,2);1+rand(5,2)]; % some data
c = [.5 .5; 1.5 1.5]; % the cluster centroids I want
d = pdist2(x,c); % compute distances from each point to each centroid
[~,cc] = min(d,[],2); % get the centroid that achieves the minimum

-- Tom

Subject: k-means

From: Fredro

Date: 17 Sep, 2011 02:56:10

Message: 3 of 3

After sending the e-mail below, I found that:
d=dist(X,C')
accomplish the same thing like pdist2 mentioned below.
Am I right here?

Thanks, Tom for the help.

"Tom Lane" <tlane@mathworks.nospam.com> wrote in message <j509bo$bhi$1@newscl01ah.mathworks.com>...
> > [IDX,C] = kmeans(M,10) --> Getting my centroid location in C with k=10
> ...
> > [IDX,C,sumd,D] = kmeans(X,10)
> > then my C (or the centroid location) will change.
>
> Fred, it sounds like you want to compute C from M, then just assign points
> in X to one of the C rows based on the distance -- not do any re-clustering.
> I think you just want to compute the distances and find the minimum
> directly. Try this:
>
> x = [rand(5,2);1+rand(5,2)]; % some data
> c = [.5 .5; 1.5 1.5]; % the cluster centroids I want
> d = pdist2(x,c); % compute distances from each point to each centroid
> [~,cc] = min(d,[],2); % get the centroid that achieves the minimum
>
> -- Tom

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us