title: matlab image rotation code
body: <p>I'm beginner in computer vision. I'm trying to do a rotate transformation using matlab. My code is</p>
  
  <pre><code>I = imread('Koala.jpg');
  
  rows = size(I, 1);
  cols = size(I, 2);
  
  deg = 45;
  deg = deg * pi / 180;
  
  C = uint8(zeros(size(I)));
  
  mid = ceil([rows+1 cols+1] / 2);
  
  [x1, x2] = meshgrid(1:rows, 1:cols);
  M = [cos(deg) sin(deg); -sin(deg) cos(deg)];
  
  X = bsxfun(@minus, [x1(:) x2(:)], mid) * M;
  X = round(bsxfun(@plus, X, mid));
  
  x1 = X(:, 1);
  x2 = X(:, 2);
  x1(x1&lt;1) = 1;
  x2(x2&lt;1) = 1;
  x1(x1&gt;rows) = rows;
  x2(x2&gt;cols) = cols;
  X = [x1(:) x2(:)];
  
  m = 1;
  for i=1:rows
      for j=1:cols
          C(X(m, 1), X(m, 2), :) = I(i, j, :);
          m = m + 1;
      end
  end
  </code></pre>
  
  <p>This works but in the result there are many pixeles without values. I guess, when I do "X2 = X*M", the range of the image on the transformation it's not same of the source and many values lost</p>