title: Maximal likelhood decoding in MATLAB
body: <p>Given a matrix <code>X</code> of size <code>(5,3)</code>, and a vector of <code>y</code> size <code>(1,3)</code>, I need to calculate the Euclidean distance of vector <code>y</code> to all vectors in <code>X</code> and return to the minimal. </p>
  
  <p>For example </p>
  
  <pre><code>X =
  
      0.1338    0.0346    0.2961
      0.5320    0.4681    0.6784
      0.4484    0.5954    0.2847
      0.1437    0.5310    0.3946
      0.2854    0.0793    0.8621
  
  y = 0.4484    0.5954    0.2847
  </code></pre>
  
  <p>So in that case, the minimal Euclidean distance of <code>y</code> is with row 3 in matrix <code>X</code>since it's the same. </p>
  
  <p>I have made the code as below : </p>
  
  <pre><code>X = rand(5,3)                %The matrix x 
  y = rand(1,3)                %The vector y
  
  [~, size_y] = size(y)        %size of y
  [size_x, ~] = size(X)        %size of matrix x 
  
  min_distance = zeros(size_x,size_y);   %Initialize the minimal distance 
  
   %% Calculate minimum distance square of vector y to every vector in x
  
     for i = 1 : size_y
          min_distance(:,i) = sum(abs(repmat(y(:,i),1,size_x) - X).^2,2); 
  end 
  
  min_distance_1 = min_distance;
  [index, ~] = (min(min_distance_1,[],1)); 
  
  results = index - 1; 
  </code></pre>
  
  <p>The results of that code is mismatch error,  however the index of row in matrix <code>X</code> which has the minimal Euclidean distance to vector <code>y</code> should be shown!! </p>
  
  <p>Is there any mistake in code ? or how can I do that ? </p>