MatLAb:警告:无法在1次迭代中收敛

时间:2014-12-02 19:20:55

标签: matlab

%% 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

我收到错误:

Warning: Failed to converge in 1 iterations. 
> In kmeans>loopBody at 395
  In smartForReduce at 128
  In kmeans at 299
  In hw4 at 48 
>> 

这可能是什么想法?

1 个答案:

答案 0 :(得分:1)

kmeans使用迭代算法来达成解决方案。您收到警告(而不是错误),因为您设置MaxIter = 1并且算法仅在一步之后没有收敛。如果您将kmeans调用更改为以下内容,则您的程序可以正常运行:

[p,mvec]=kmeans(clustertot',ncl);

您是否有理由加入MaxIter选项?