Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Kmeans procedure for complex numbers

731 views
Skip to first unread message

Jan Degirmendžić

unread,
Oct 14, 2004, 8:58:49 AM10/14/04
to
In Matlab "kmeans(X,k)" procedure, when X is a complex matrix,
produces negative within-cluster sums of point-to-centroid distances
("sumd").
Why?

Temu Gautama

unread,
Oct 14, 2004, 10:16:08 AM10/14/04
to

The squared Euclidean distance between x and y is defined as
sum((x-y).^2), so if x and y are complex-valued, the distance can be
complex-valued as well. Example:

x = sqrt(randn(5,1)); y = sqrt(randn(5,1));
sum((x-y).^2)

Now, if x and y don't have a real part, then the distance becomes
negative.

x = sqrt(-rand(5,1)); y = sqrt(-rand(5,1));
sum((x-y).^2)

Temu

Peter Perkins

unread,
Oct 14, 2004, 4:52:18 PM10/14/04
to
Jan Degirmendžić wrote:

Hi Jan -

To use complex data with KMEANS, the simplest thing to do is split out its
real and complex parts to get real data in twice as many dimensions.
Alternatively, you could find the two lines (lines 639 and 460 in the R14
version) where the squared Euclidean distance is calculated, and modify them
from something like

sum((X - C).^2, 2)

to something like

sum(abs(X - C).^2, 2).

Hope this helps.

- Peter Perkins
The MathWorks, Inc.

Jiaxin Yang

unread,
Nov 13, 2014, 12:22:18 PM11/13/14
to
Dear Mathworks,

In order to adapt the "kmeans" function to complex number data, could you please let know how I can modify the function in R2014a version?

Thanks in advance.

Peter Perkins <Peter.Perki...@mathworks.com> wrote in message <ckmou2$8qi$1...@fred.mathworks.com>...

kawtherhamad99

unread,
Dec 1, 2014, 9:56:54 PM12/1/14
to
Can you please help me how to find the decision boundary between 4 clusters in matlab. I have the below code, i want to find the decsion boundaries, i want to find then the bet error rate. Can i consider the data as training data? if yes, how can i then implement the test data?

clear all
clc
load fisheriris
labels = (1:1200);
T=[ 2+2*i 2-2*i -2+2*i -2-2*i];

A=randn(150,2)+2*ones(150,2); C=randn(150,2)-2*ones(150,2);
B=randn(150,2)+2*ones(150,2); F=randn(150,2)-2*ones(150,2);
D=randn(150,2)+2*ones(150,2); G=randn(150,2)-2*ones(150,2);
E=randn(150,2)+2*ones(150,2); H=randn(150,2)-2*ones(150,2);

X = [A; B; D; C; F; E; G; H];


[idx, centroids] = kmeans(X, 4, 'Replicates', 20);

x = X(:,1);
y = X(:,2);

classifierType = 'quadratic'; %# 'quadratic', 'linear'
npoints=100;
mn = min(X); mx = max(X);
[I,J] = meshgrid( linspace(mn(1),mx(1),npoints), linspace(mn(2),mx(2),npoints));
[K,L] = meshgrid( linspace(mn(1),mx(1),npoints), linspace(mn(2),mx(2),npoints));
I = I(:); J = J(:); K = K(:); L = L(:);
[C,err,P,logp,coeff] = classify([I J K L], X, labels, classifierType);

figure;
hold on;
colors = 'rgbk';
for num = 1 : 4
plot(x(idx == num), y(idx == num), [colors(num) '.']);
end

plot(centroids(:,1), centroids(:,2), 'c.', 'MarkerSize', 14);
NUM_K=4;
%# draw decision boundaries between pairs of clusters
for i=1:NUM_K
for j=i+1:NUM_K
if strcmp(coeff(i,j).type, 'quadratic')
K = coeff(i,j).const;
L = coeff(i,j).linear;
Q = coeff(i,j).quadratic;
f = sprintf('0 = %g + %g*x + %g*y + %g*x^2 + %g*x.*y + %g*y.^2',...
K,L,Q(1,1),Q(1,2)+Q(2,1),Q(2,2));
else
K = coeff(i,j).const;
L = coeff(i,j).linear;
f = sprintf('0 = %g + %g*x + %g*y', K,L(1),L(2));
end
h2 = ezplot(f, [mn(1) mx(1) mn(2) mx(2)]);
set(h2, 'Color','k', 'LineWidth',2)
end
end
grid;


Thank you.
Kawther


0 new messages