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
?
q
instead ofk
as the number of clusters? It means you only have 2 clusters and not 4 where in your answer you're pointing out that the decimal value could be 1, 2, 3, or 4 which represents the number of clusters as 4. Also, the decimal to binary conversion works fine. It's quite vague, I think, you need to change the way you want to encode thekmeans
results!q
because there are 2 clusters or 2 symbols. Each element inx1
array will either belong to cluster 1 or cluster 2.As a result, an array of 4 elements containing symbols 1,2 would be produced. This is the variableidx_centers
. I treat this array as a string and replace the symbol2
with0
in order to get a binary stringBinaryCode_KMeans
. I convert this string to its decimal equivalent which should either be 1,2,3 or 4. Basically, I want the decimal number to be in the range ofk
. For this, how should I cluster, what should beq
inkmeans
kmeans
, it can produceidx_centers = [1 2 2 2]
oridx_centers = [2 1 1 1]
, then the binary string would bestring = [1 0 0 0]
orstring = [0 1 1 1]
and the decimal value would be8
or7
. How would you interpret these results because you can't have a decimal value between 1 to 4 here?k
usingq
bits -- how many elements should be in the data arrayx1
and what should be the input tokmeans
so that after converting the clusters symbols, I can get a decimal representation that is in the rangek
?