function Q_Matrix = Q_ML(SS,EbN0dB,n)
% Q_ML evauates the (symbol) transition probabilities under AWGN 
% channel whose PSD is N0/2. Minimum distance decoding is used at the 
% receiver to implement ML detector. 
% Q_ML needs three inputs: (1) the matrix SS whose columns are the
% possible transmitted vectors, (2) a value of Eb/N0 (in dB) 
% of interest, and (3) the number n of samples to run.

%% Energy Calculation
M = size(SS,2);
K = size(SS,1);
ES = sum(sum(SS.^2))/M;
Eb = ES/log2(M);
% Calculate the amount of N0 which corresponds to the specified value of
% Eb/N0
EbN0 = 10.^(EbN0dB/10); N0 = 1./EbN0.*Eb; 
Q_Matrix = [];

for w = 1:M
    % Transmitted Vectors (Points in the constellation)
    S = SS(:,w)*ones(1,n);
    % Note that instead of randomly select the points to be transmitted,
    % here, we want to evaluate the conditional probability and hence we
    % simply generate the same symbol over and over again to find what they
    % becomes at the receivers. 
    %% Channel
    % Generate gaussian noise whose variance in each (real) dimension is N0/2
    sigma = sqrt(N0/2);
    N = random('Normal',0,sigma,size(S));   
    % Received Vector    
    R = S+N;
    %% Minimum Distance Decoder
    D2 = []; % A trix to save the distance values
    %R
    for i = 1:M
        D = sum((bsxfun(@minus,R,SS(:,i))).^2,1);
        D2 = [D2; D];
    end
    [minD W_hat]  = min(D2); % Find the index that has the min distance
    Qt = (hist(W_hat,1:M))/n;
    Q_Matrix = [Q_Matrix; Qt];
end