-7
int max = 0, id = 0;
int indx= 0;

vector<int> clusters(k,0);

for (size_t i = 0; i < bestLabels.size(); i++)
{
    id = bestLabels[i];
    clusters[id]++;

    if (clusters[id] > max)
    {
        max = clusters[id];
        indx = id;
    }
}

This code calculates for the largest cluster through K-Means Clustering but i don't quite understand how "clusters[id]++;" and "cluster[id]" work. What do they do exactly? Can anyone please give a detailed explanation on the process happening inside the for loop? Any help would be highly appreciated. Thank you!

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
  • Possible duplicate : [The Definitive C++ Book Guide and List](http://stackoverflow.com/q/388242/327083) – J... Jan 26 '17 at 00:04

2 Answers2

1

The following line:

vector<int> clusters(k,0);

Defines a random-access collection of k total integers each with an initial value of 0. clusters[id] accesses the integer value stored at index id in the vector. clusters[id]++ increments the integer value stored at index id. This works because operator [] on a vector returns a reference to the indexed item, allowing modification.

Mikel F
  • 3,567
  • 1
  • 21
  • 33
  • oh i see. it makes more sense to me now. i just got confused because of the "clusters" variable having two parameters and increments it with the variable "id". Thank you, Mikel! – littleMissProgrammer Jan 26 '17 at 00:14
0

The for loop iterates through the labels in bestLabels and scores them based on how often they appear (clusters[id]++ - this part increases the score, or frequency). It also keep track of the most frequent label - that's what the clause of the if (clusters[id] > max) condition does.

holygeek
  • 15,653
  • 1
  • 40
  • 50