title: How can I Convert MATLAB code into C#
body: <p>I have a MATLAB code that does facial recognition on an image by first converting the image into a gray image and assigning image to a 1D array
  I need to convert this code into C#.</p>
  
  <p>This Function reads the images:
  <div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>function T = CreateDatabase(TrainDatabasePath)
  
  %%%%%%%%%%%%%%%%%%%%%%%% File management
  TrainFiles = dir(TrainDatabasePath);
  Train_Number = 0;
  
  for i = 1:size(TrainFiles,1)
      if not(strcmp(TrainFiles(i).name,'.')|strcmp(TrainFiles(i).name,'..')|strcmp(TrainFiles(i).name,'Thumbs.db'))
          Train_Number = Train_Number + 1; % Number of all images in the training database
      end
  end
  
  %%%%%%%%%%%%%%%%%%%%%%%% Construction of 2D matrix from 1D image vectors
  T = [];
  for i = 1 : Train_Number
      
      % I have chosen the name of each image in databases as a corresponding
      % number. However, it is not mandatory!
      str = int2str(i);
      str = strcat('\',str,'.jpg');
      str = strcat(TrainDatabasePath,str);
      
      img = imread(str);
      img = rgb2gray(img);
      
      [irow icol] = size(img);
     
      temp = reshape(img',irow*icol,1);   % Reshaping 2D images into 1D image vectors
      T = [T temp]; % 'T' grows after each turn                    
  end</code></pre>
  </div>
  </div>
  </p>
  
  <p>Getting Eigen values</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>function [m, A, Eigenfaces] = EigenfaceCore(T)
                  
   
  %%%%%%%%%%%%%%%%%%%%%%%% Calculating the mean image 
  m = mean(T,2); % Computing the average face image m = (1/P)*sum(Tj's)    (j = 1 : P)
  Train_Number = size(T,2);
  
  %%%%%%%%%%%%%%%%%%%%%%%% Calculating the deviation of each image from mean image
  A = [];  
  for i = 1 : Train_Number
      temp = double(T(:,i)) - m; % Computing the difference image for each image in the training set Ai = Ti - m
      A = [A temp]; % Merging all centered images
  end
  
  %%%%%%%%%%%%%%%%%%%%%%%% Snapshot method of Eigenface methos
  % We know from linear algebra theory that for a PxQ matrix, the maximum
  % number of non-zero eigenvalues that the matrix can have is min(P-1,Q-1).
  % Since the number of training images (P) is usually less than the number
  % of pixels (M*N), the most non-zero eigenvalues that can be found are equal
  % to P-1. So we can calculate eigenvalues of A'*A (a PxP matrix) instead of
  % A*A' (a M*NxM*N matrix). It is clear that the dimensions of A*A' is much
  % larger that A'*A. So the dimensionality will decrease.
  
  L = A'*A; % L is the surrogate of covariance matrix C=A*A'.
  [V D] = eig(L); % Diagonal elements of D are the eigenvalues for both L=A'*A and C=A*A'.
  
  %%%%%%%%%%%%%%%%%%%%%%%% Sorting and eliminating eigenvalues
  % All eigenvalues of matrix L are sorted and those who are less than a
  % specified threshold, are eliminated. So the number of non-zero
  % eigenvectors may be less than (P-1).
  
  L_eig_vec = [];
  for i = 1 : size(V,2) 
      if( D(i,i)&gt;1 )
          L_eig_vec = [L_eig_vec V(:,i)];
      end
  end
  
  %%%%%%%%%%%%%%%%%%%%%%%% Calculating the eigenvectors of covariance matrix 'C'
  % Eigenvectors of covariance matrix C (or so-called "Eigenfaces")
  % can be recovered from L's eiegnvectors.
  Eigenfaces = A * L_eig_vec; % A: centered image vectors</code></pre>
  </div>
  </div>
  </p>
  
  <p>Recognition </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>function OutputName = Recognition(TestImage, m, A, Eigenfaces)
  
  
                 
  
  %%%%%%%%%%%%%%%%%%%%%%%% Projecting centered image vectors into facespace
  % All centered images are projected into facespace by multiplying in
  % Eigenface basis's. Projected vector of each face will be its corresponding
  % feature vector.
  
  ProjectedImages = [];
  Train_Number = size(Eigenfaces,2);
  for i = 1 : Train_Number
      temp = Eigenfaces'*A(:,i); % Projection of centered images into facespace
      ProjectedImages = [ProjectedImages temp]; 
  end
  
  %%%%%%%%%%%%%%%%%%%%%%%% Extracting the PCA features from test image
  InputImage = imread(TestImage);
  temp = InputImage(:,:,1);
  
  [irow icol] = size(temp);
  InImage = reshape(temp',irow*icol,1);
  Difference = double(InImage)-m; % Centered test image
  ProjectedTestImage = Eigenfaces'*Difference; % Test image feature vector
  
  %%%%%%%%%%%%%%%%%%%%%%%% Calculating Euclidean distances 
  Euc_dist = [];
  for i = 1 : Train_Number
     q = ProjectedImages(:,i);
      temp = ( norm( ProjectedTestImage - q ) )^2;
      Euc_dist = [Euc_dist temp];
  end
  [Euc_dist_min , Recognized_index] = min(Euc_dist);
  OutputName = strcat(int2str(Recognized_index),'.jpg');
  
  
  %%%%%%%%%%%%%%%%%%%%%%%% Calculating Mahanalobis %distances 
  %maha_dist = [];
  %X = []; 
  %Y = [];
  %for i = 1 : Train_Number
    % S = cov(X);
   %  mu = mean(X);
  %temp = (Y(i,:)-mu)*inv(S)*(Y(i,:)-mu)';
  
  %maha_dist = [maha_dist temp];
  %end
  
  %[maha_dist_min , Recognized_index] = min(maha_dist);
  %OutputName = strcat(int2str(Recognized_index),'.jpg');</code></pre>
  </div>
  </div>
  </p>