How to combine the number of matrix elements used in find matlab function

I wrote a function to assign training examples to their nearest centroids as part of the K-mean clustering algorithm. It seems to me that the dimensions are met and the code is working correctly. But often I get an error

In task A (:) = B, the number of elements in and B should be the same.

for line

idx(i) = find(dist == value); 

Here is the code

 function idx = findClosestCentroids(X, centroids) K = size(centroids, 1); idx = zeros(size(X,1), 1); dist = zeros(K, 1); for i = 1:size(X,1) for j = 1:K dist(j) = sum((X(i,:) - centroids(j,:)).^2); end value = min(dist); idx(i) = find(dist == value); end 

What is the problem?

+5
source share
1 answer

This is because you potentially find more than one cluster that have the same distance to the query point. find defines all values ​​that satisfy the logical condition as an argument. idx(i) implies that you assign a single value to the idx array, but find can give more than one value, and this gives the assignment error that you see.

Instead, use the second output argument min , which defines the index for the first time , the smallest value that you want to execute:

 function idx = findClosestCentroids(X, centroids) K = size(centroids, 1); idx = zeros(size(X,1), 1); dist = zeros(K, 1); for i = 1:size(X,1) for j = 1:K dist(j) = sum((X(i,:) - centroids(j,:)).^2); end [~,idx(i)] = min(dist); %// Change end 
+4
source

Source: https://habr.com/ru/post/1240662/


All Articles