Matlab : Binary to decimal conversion using symbols from clustering algorithm

259 Views Asked by At
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?

1

There are 1 best solutions below

7
On BEST ANSWER

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 and n the number of samples. There are three ways in the code below to encode this information.

clear
clc

q = 2;
k= 2^q;
n = 4;
x1 = rand(n,1);
fprintf('x1 = [ '); fprintf('%d ', x1); fprintf(']\n');

[idx_centers, location] = kmeans(x1, q);
fprintf('idx_centers = [ '); fprintf('%d ', idx_centers); fprintf(']\n');

for i = 1:q
    idx_centers(idx_centers == i) = i-1;
end

fprintf('idx_centers = [ '); fprintf('%d ', idx_centers); fprintf(']\n');

string = num2str(idx_centers');

% Original decimal value
DecX = bin2dec(string);
fprintf('0 to     (2^n) - 1: %d\n', DecX);

% Reduced space decimal value
% Ignoring the 0/1 order as [ 1 1 0 0 ]
% would be the same      as [ 0 0 1 1 ]
if DecX >= (2^n)/2
    complement = bitget(bitcmp(int64(DecX)),n:-1:1);
    DecX = bin2dec(num2str(complement));
end
fprintf('0 to ((2^n)/2) - 1: %d\n', DecX);

% Minimal Decimal value based on the number of samples  
% in the 0's cluster which is in the range of 0 to n-1
fprintf('0 to         n - 1: %d\n', numel(find(idx_centers == 0)));

Hint: If you change the q to more than 2, the code will not work because bin2dec 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.