Reputation: 40464
%% clear/load
clc; clear all; close all; % removes variables, clears console
%% const, values do not change
fdim = 2; % data dimension
iter = 5; % number of em algorithm iterations
ncl = 3; % number of clusters
stype = 'full';
% covariance matrix type < spherical, diagonal, full >
%% artificial data
%% true mixture parameters & generate observations
% m stands for mu, μ (mean)
% cluster mean
m1 = [2,3];
m2 = [5,8];
m3 = [9,4];
% sig stands for sigma, σ (standard deviation)
% cluster variances
sig1 = [2.5,0.6;0.6,2.5];
sig2 = [2.2,1.1;1.1,2.2];
sig3 = [3,-1.7;-1.7,3];
% cluster weight (number of observations)
N1 = 1000;
N2 = 600;
N3 = 400;
% multivariate normal random numbers
% syntex
% R = mvnrnd(MU,SIGMA)
% r = mvnrnd(MU,SIGMA,cases)
cluster1 = mvnrnd(m1, sig1, N1)';
cluster2 = mvnrnd(m2, sig2, N2)';
cluster3 = mvnrnd(m3, sig3, N3)';
% whole observations
clustertot=[cluster1 cluster2 cluster3];
%% Initialized by K-means
len_data = length(clustertot); % data length
% k-means clustering
% syntex
% idx = kmeans(X,k,Name,Value)
% initial parameter estimation (using k-means)
[p,mvec]=kmeans(clustertot',ncl,'Maxiter',1); % <--------------------------- error
I get the error:
Warning: Failed to converge in 1 iterations.
> In kmeans>loopBody at 395
In smartForReduce at 128
In kmeans at 299
In hw4 at 48
>>
Any ideas as what this can be?
Upvotes: 0
Views: 2292
Reputation: 626
kmeans uses an iterative algorithm to reach a solution. You are getting a warning (not an error) because you set MaxIter = 1 and the algorithm hasn't converged after only one step. If you change the kmeans call to the following, your program works:
[p,mvec]=kmeans(clustertot',ncl);
Did you have a reason for including the MaxIter option?
Upvotes: 1