Note that there are some explanatory texts on larger screens.

plurals
  1. POFitting the Cluster Number Based on the Centroid
    text
    copied!<p>I'm working with k-means in MATLAB. Here is my code:</p> <pre><code>load cobat.txt; % read the file k=input('Enter a number: '); % determine the number of cluster isRand=0; % 0 -&gt; sequeantial initialization % 1 -&gt; random initialization [maxRow, maxCol]=size(cobat); if maxRow&lt;=k, y=[m, 1:maxRow]; else % initial value of centroid if isRand, p = randperm(size(cobat,1)); % random initialization for i=1:k c(i,:)=cobat(p(i),:) ; end else for i=1:k c(i,:)=cobat(i,:); % sequential initialization end end temp=zeros(maxRow,1); % initialize as zero vector u=0; while 1, d=DistMatrix3(cobat,c); % calculate the distance [z,g]=min(d,[],2); % set the matrix g group if g==temp, % if the iteration doesn't change anymore break; % stop the iteration else temp=g; % copy the matrix to the temporary variable end for i=1:k f=find(g==i); if f % calculate the new centroid c(i,:)=mean(cobat(find(g==i),:),1) end end c sort(c) end y=[cobat,g] </code></pre> <p>"cobat" is the file of mine. Here it looks:</p> <pre><code>65 80 55 45 75 78 36 67 66 65 78 88 79 80 72 77 85 65 76 77 79 65 67 88 85 76 88 56 76 65 </code></pre> <p>"c" is the variable of centroid (the central of the cluster) per each cluster. "g" is the variable to show the cluster number. The problem is, I want to sort/fit the cluster number (small to big) based on the centroid (c). So, I try to sort(c), but it doesn't affect to the cluster number (g).</p> <p>When I try to sort(g), it's sorted just not like what I want. I want the cluster number is sorted based on the centroid. Example; when I run the code with k=3, here is the final centroid</p> <pre><code> 73.0000 79.0000 70.6667 %C 1 58.3333 73.3333 84.6667 %C 2 36.0000 67.0000 66.0000 %C 3 </code></pre> <p>When I sort it, the number cluster is also "sorted",</p> <pre><code>36.0000 67.0000 66.0000 %C 3 58.3333 73.3333 70.6667 %C 2 73.0000 79.0000 84.6667 %C 1 </code></pre> <p>I want it the number cluster is fit, like this.</p> <pre><code>36.0000 67.0000 66.0000 %C 1 58.3333 73.3333 70.6667 %C 2 73.0000 79.0000 84.6667 %C 3 </code></pre> <p>It's fit, not sorted, so when this line 'y=[cobat,g]' is run, it changes too.</p> <p>This seems easy, but tricky. I couldn't figure out. Anyone have any idea to solve it?</p> <p>Thank you.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload