please help, anybody explain me the meaning of following code

distance = ((vector- vec_mean).^2);
vec_mean(2:end,:)=[];
[~,label_vector] = min(distance,[],2);
% i=1:no_of_cluster;
for i=1:no_of_cluster
index=(label_vector==i);
vec_mean(:,i)=sum(vector(index))/nnz(index);
end

3 Comments

This code is incomplete, contains no comments or explanations. What exactly do you expect other users here to explain?
Follow the algorithm and you will find out what it does. Try it with some values, and use some debugging tools to help see what happens to those values. Use MATLAB's help to understand how indexing, function calls and for loops work.
~ in that context just means ignore that output from the function.
You need to ask more specific questions though really. If you can't understand anything at all in that code then you need to read the basic Matlab help. If you can understand most of it then ask about the specific things you do not understand.
Based on your response, you should read MATLAB's own introduction to programming concepts . You will find explanations, examples and other interesting info to read... just navigate around using the help's contents browser on the left hand side of the window.
To help you to understand that code, start with the sections "Language Fundamentals" and "Programming Scripts and Functions".

Sign in to comment.

 Accepted Answer

I don't really know what you want explaining, but it doesn't look like the code can run successfully.
index=(label_vector==i);
will return a logical 0 or 1 which then appears to be used as an index into an array which will be impossible in all case except where label_vector == i.

2 Comments

logical arrays can be used to index into other arrays just fine. You just get the element of the array where the logical array is true.
Actually, assuming vector and vec_mean are m x n matrices (the 2nd line of code would indicate so), label_vector and thus index are m x 1 arrays. index can be used just fine to get elements of vector
Because index is only as long as the number of rows in vector, sum(vector(index)) is only the sum of the elements of the 1st column of _vector where index is true. If that is indeed what is intended, it would be much clearer to be explicit about the column:
vec_mean(:, i) = sum(vector(index, 1)) / nnz(index);
Note that the expression is equivalent to:
vec_mean(:, 1) = mean(vector(index, 1));
Ah yes, of course, I've spent too long lately looking at my class objects' properties showing 0 where I had input a 'false' and forgetting it still behaves as a logical!

Sign in to comment.

More Answers (0)

Categories

Asked:

on 22 Sep 2014

Commented:

on 22 Sep 2014

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!