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

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

Undefined function or variable "idxBest" in kmeans

Asked by thi on 25 Feb 2013

Hello,

I have been using kmeans function from the statistics toolbox (with matlab R2012a on both windows and macos). And it often (not always, depending on the data) crashes with the error:

Undefined function or variable "idxBest".
Error in kmeans(line 332)
idx = idxBest;

The function kmeans is called with the following option:

[indNew_nodes ctrs] = kmeans(elem,k,'emptyaction','drop');

where k = 15.

I have been googleing this error, and people have reported problems when elem has NAN values. But it is not the case here.

For instance, in the last crash, elem is a 2000*1 vector:

-100.0000
 -100.0000
    1.8656
    1.8656
    1.8656
    1.8656
 -100.0000
 -100.0000
    4.9147
 -100.0000
 -100.0000
    4.9147
 -100.0000
 -100.0000
 -100.0000
    4.9147
    4.9147
    4.0652
 -100.0000
    4.0652
    4.0652
 -100.0000
 -100.0000
 -100.0000
 -100.0000
    4.0652
    4.0652
 -100.0000
    4.0652
 -100.0000
 -100.0000
    4.0652
    4.0652
 -100.0000
 -100.0000
 -100.0000
 -100.0000
 -100.0000
 -100.0000
 -100.0000
    4.0652
 -100.0000
    4.0652
 -100.0000
    4.0652
 -100.0000
    4.0652
    4.0652
    4.0652
    4.0652
 -100.0000
    4.0652
    4.0652
 -100.0000
    4.0652
    4.0652
    4.0652
 -100.0000
    1.8656
 -100.0000
    1.8656
    1.8656
    1.8656
    1.8656
 -100.0000
    1.8656
    1.8656
    4.9147
    4.9147
    4.9147
    4.9147
 -100.0000
    4.9147
    4.9147
 -100.0000
 -100.0000
 -100.0000
    4.0652
    4.0652
    4.0652
 -100.0000
    4.0652
 -100.0000
    4.0652
 -100.0000
 -100.0000
 -100.0000
 -100.0000
 -100.0000
 -100.0000
    4.0652
 -100.0000
    4.0652
    4.0652
 -100.0000
    4.0652
    4.0652
 -100.0000
 -100.0000
    4.0652
    4.0652
    4.0652
    4.0652
 -100.0000
 -100.0000
 -100.0000
 -100.0000
    4.0652
    4.9147
    4.9147
    4.9147
 -100.0000
    4.9147
    4.9147
    4.9147
    4.9147
    4.9147
    4.0652
    4.0652
    4.0652
    4.0652
    4.0652
    4.0652
    4.0652
 -100.0000
    4.0652
    4.0652
    4.0652
    4.0652
 -100.0000
 -100.0000
    4.0652
 -100.0000
 -100.0000
 -100.0000
 -100.0000
 -100.0000
 -100.0000
 -100.0000
 -100.0000
 -100.0000
    4.0652
 -100.0000
 -100.0000
    4.0652
 -100.0000
    4.0652
    4.0652
    4.0652
    4.0652
    4.0652
 -100.0000
    4.0652
 -100.0000
    4.0652
 -100.0000
 -100.0000
 -100.0000
    4.9147
    4.9147
    4.9147
    4.9147
    4.9147
    4.9147
 -100.0000
    4.9147
    4.9147
 -100.0000
    1.1611
 -100.0000
    1.1611
 -100.0000
    1.1611
    1.1611
 -100.0000
    1.1611
 -100.0000
    1.8656
 -100.0000
 -100.0000
    1.8656
 -100.0000
 -100.0000
 -100.0000
    4.8754
 -100.0000
 -100.0000
 -100.0000
 -100.0000
 -100.0000
    1.8656
    1.8656
 -100.0000
    1.8656
    1.8656
    1.8656
    1.8656
 -100.0000
    1.8656

Can anybody help and tell me why I get these errors?

0 Comments

thi

Tags

2 Answers

Answer by Sean de Wolski on 25 Feb 2013
which -all kmeans

It looks like you have something shadowing the Statistics Toolbox's kmeans()

1 Comment

thi on 1 Mar 2013

no, it is the kmeans of the Statistics Toolbox. I since then made a copy in my working folder of kmeans.mat to edit it, but it is the same code.

Sean de Wolski
Answer by Tom Lane on 25 Feb 2013

This looks like a bug. I can see it has been fixed in the toolbox source code for distribution in an upcoming release. From what I can tell, it can only happen if you use 'drop' as the value of 'EmptyAction'. Would it be satisfactory to use 'singleton' instead? If not, and you are willing to edit the kmeans.m file, you could change this line:

totsumD = sum(sumD); 

to this:

totsumD = sum(sumD(nonempties)); 

2 Comments

thi on 1 Mar 2013

Thank you. It seems that 'singleton' no longer makes it crash as often (but still does from time to time). In this case, it create a new cluster consisting of the one point furthest from its centroid. But what is the difference, what is the difference? It is hard for me to assess the consequences on my program...

Tom Lane on 1 Mar 2013

You are right, if a cluster disappears, the function seeks to maintain the requested number of clusters by selecting the point as you describe.

If you see an error using 'singleton', especially if you've edited the file, I'd welcome an e-mail from you with instructions on how to reproduce that.

Tom Lane

Contact us