title: RGB to norm rgb transformation. Vectorizing
body: <p>I'm writing a piece of code that has to transform from an RGB image to an rgb normalized space. I've got it working with a <em>for</em> format but it runs too slow and I need to evaluate lots of images. I'm trying to vectorize the full function in order to faster it. What I have for the moment is the following:</p>
  
  <pre><code>     R = im(:,:,1);
       G = im(:,:,2);
       B = im(:,:,3);
  
       r=reshape(R,[],1);
       g=reshape(G,[],1);
       b=reshape(B,[],1);
  
       clear R G B;
  
       VNormalizedRed = r(:)/(r(:)+g(:)+b(:));
       VNormalizedGreen = g(:)/(r(:)+g(:)+b(:));
       VNormalizedBlue = b(:)/(r(:)+g(:)+b(:));
  
       NormalizedRed = reshape(VNormalizedRed,height,width);
       NormalizedGreen = reshape(VNormalizedGreen,height,width);
       NormalizedBlue = reshape(VNormalizedBlue,height,width);
  </code></pre>
  
  <p>The main problem is that when it arrives at <code>VNormalizedRed = r(:)/(r(:)+g(:)+b(:));</code> it displays an out of memory error (wich is really strange because i just have freed three vectors of the same size). Were is the error? (<strong>solved</strong>)</p>
  
  <p>Its possible to do the same process in a more efficiently way?</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>After using Martin sugestions I found the reshape function was not necessary, being able to do the same with a simple code:</p>
  
  <pre><code>     R = im(:,:,1);
       G = im(:,:,2);
       B = im(:,:,3);
  
       NormalizedRed = R(:,:)./sqrt(R(:,:).^2+G(:,:).^2+B(:,:).^2);
       NormalizedGreen = G(:,:)./sqrt(R(:,:).^2+G(:,:).^2+B(:,:).^2);
       NormalizedBlue = B(:,:)./sqrt(R(:,:).^2+G(:,:).^2+B(:,:).^2);
  
       norm(:,:,1) = NormalizedRed(:,:);
       norm(:,:,2) = NormalizedGreen(:,:);
       norm(:,:,3) = NormalizedBlue(:,:);
  </code></pre>