title: Nearest-neighbor interpolation algorithm in MATLAB
body: <p>I am trying to write my own function for scaling up an input image by using the Nearest-neighbor interpolation algorithm. The bad part is I am able to see how it works but cannot find the algorithm itself. I will be grateful for any help.</p>
  
  <p>Here's what I tried for scaling up the input image by a factor of 2:</p>
  
  <pre><code>function output = nearest(input)
  [x,y]=size(input);
  output = repmat(uint8(0),x*2,y*2);
  [newwidth,newheight]=size(output);
  for i=1:y
      for j=1:x
          xloc = round ((j * (newwidth+1)) / (x+1));
          yloc = round ((i * (newheight+1)) / (y+1));
          output(xloc,yloc) = input(j,i);
      end
  end
  </code></pre>
  
  <p>Here is the output after <a href="https://stackoverflow.com/users/5987/mark-ransom">Mark</a>'s suggestion <img src="https://i.stack.imgur.com/KnNpF.png" alt="alt text"></p>