q = 2;
k= 2^q;
x1 = [0.0975000000000000, 0.980987500000000, -0.924672950312500, -0.710040130079246];
for i = 1 : length(x1)
[idx_centers,location] = kmeans(x1',q);
end
temp = idx_centers;
for i = 1 : length(x1)
if temp(i)== 2
idx_centers(i) = 0;
end
BinaryCode_KMeans(i) = idx_centers(i); % output is say [0,0,1,1];
end
strng = num2str(BinaryCode_KMeans);
DecX = bin2dec(strng);
In the above code snippet, I want to express the binary string to its decimal equivalent where the binary string is obtained from kmeans
clustering. The decimal equivalent should either be 1,2,3, or 4 i.e., k = 2^q
when q=2
.
But sometimes after conversion, the decimal equivalent is 12 because for a 4 bit binary code we get decimal numbers in 1 to 16 or 0 -- 15. the number of elements in x1
can vary and can be less than or greater than k
. What should I do so that I can always get the decimal equivalent of the binary code within k
for any value of q
?
First of, there is no need to run
kmeans
multiple times, it will calculate the cluster centers using a single run. Note that, the code below tries to find a mapping between the clustering results andn
the number of samples. There are three ways in the code below to encode this information.Hint: If you change the
q
to more than 2, the code will not work becausebin2dec
only accepts zeros and ones. In case of having more than 2 clusters, you need to elaborate the code and use multidimensional arrays to store the pairwise clustering results.