title: Transporting vectorized Matlab code to python, numpy
body: <p>I am transporting my matlab code to python. There are alot of things that I am trying to find replacements for in python and numpy</p>
  
  <p>Matlab Code:</p>
  
  <pre><code>    [m,n]=size(Image);
  
      canvas=zeros(m,n);
  
      U_res_sel=squeeze(loading);
      [s1,s2]=size(U_res_sel);
  
      if mod(s1,2)==0
          dy=s1/2-1;
      else
          dy=(s1-1)/2;
      end
      if mod(s2,2)==0
          dx=s2/2-1;
      else
          dx=(s2-1)/2;
      end
  
      xmin=dx+1;
      ymin=dy+1;
      xmax=n-(s2-dx-1);
      ymax=m-(s1-dy-1);
  
      [x,y]=meshgrid(xmin:xmax,ymin:ymax);
  
      ind=sub2ind([m,n],y(:),x(:));
  
      nps=repmat(((-dx+(0:s2-1))*m-dy),s1,1)+repmat((0:(s1-1)).',1,s2);
  
      ind=repmat(ind,1,numel(nps))+repmat(nps(:).',numel(ind),1);
  
      A=(Image(ind)-repmat(mean(Image(ind),2),1,numel(nps)));
  
      B=repmat((U_res_sel(:)-mean(U_res_sel(:))).',size(ind,1),1);
  
      canvas(ymin:ymax,xmin:xmax)=reshape(sum(A.*B,2)./sqrt(sum((A.^2),2).*sum((B.^2),2)),ymax-ymin+1,[]);
  
      canvas = canvas(y1+1:z1+y1+1,y2+1:z2+y2+1);
  </code></pre>
  
  <p>I guess i will explain line by line where problems are occurring.</p>
  
  <p>i am using </p>
  
  <p>import numpy as np</p>
  
  <p>for the numpy functions</p>
  
  <p>1.</p>
  
  <p>The variables work fine until i reach the meshgrid line in python.</p>
  
  <pre><code>    [x,y] = np.mgrid[xmin:xmax,ymin:ymax]
  </code></pre>
  
  <p>x in matlab has a size 517,517 using test data
  x in python has a size has 516 by 516 so i changed the python section of the code with </p>
  
  <pre><code>    xmax=n-(s2-dx-1) + 1
      ymax=m-(s1-dy-1) + 1
  
      [y,x] = np.mgrid[xmin:xmax,ymin:ymax]
  </code></pre>
  
  <p>to make it the same data sets as the matlab code. But I am not sure if the indexing is sound. If i have the exact same matrix in matlab to numpy array are they equivalent?</p>
  
  <p>2.</p>
  
  <p>The next matlab line is a mess for me to figure out.</p>
  
  <pre><code>    ind=sub2ind([m,n],y(:),x(:));
  </code></pre>
  
  <p>for the x(:) and y(:) i am using</p>
  
  <pre><code>    x = np.reshape(x,(x.size,1))
      y = np.reshape(y,(y.size,1))
      x = np.int64(x)
      y = np.int64(y)
  </code></pre>
  
  <p>for the sub2ind function i am using in python</p>
  
  <pre><code>    ind = np.ravel_multi_index((y,x), dims=(m,n) )
  </code></pre>
  
  <p>but here is where the numbers mess up. </p>
  
  <p>in matlab i get a column vector with a range 3723 to 278760 for a certain data set
  and for the same data set 
  in python i get a column vector of 4264 to 279292 with a different stepping in between.</p>
  
  <p>they both are the same size of (267289,1) though.</p>
  
  <p>3.</p>
  
  <p>This line i got working fine in matlab and python I am just putting it here so i can be concise to myself.</p>
  
  <p>matlab:</p>
  
  <pre><code>    nps=repmat(((-dx+(0:s2-1))*m-dy),s1,1)+repmat((0:(s1-1)).',1,s2);
  </code></pre>
  
  <p>python:</p>
  
  <pre><code>    dtx = (-dx + np.arange(0,s2,1))
      dtx_2 = np.arange(0,s1,1)
      dtx_2 = np.reshape(dtx_2,(dtx_2.size,1))
  
      nps = np.tile(   dtx*m-dy,(s1,1)  ) + np.tile(   dtx_2  ,(1,s2)  )
  </code></pre>
  
  <p>(4). </p>
  
  <p>for line in matlab</p>
  
  <pre><code>    ind=repmat(ind,1,numel(nps))+repmat(nps(:).',numel(ind),1);
  </code></pre>
  
  <p>in python i am trying</p>
  
  <pre><code>    a = np.tile(ind,(1,nps.size))
      b = np.tile( np.transpose(dtind) , (ind.size,1) )
      ind = a + b
  </code></pre>
  
  <p>I split it up into a and b to make it more readable.</p>
  
  <p>But nothing in this is really working as intended.</p>
  
  <p>(5). </p>
  
  <p>I am unsure how to access a variable by index in python.</p>
  
  <p>In matlab i could just do Image(ind), but is my code useless now in python because i can't find an alternative to this? </p>
  
  <p>A bit of a note if you try running the matlab code, is that it will cause your computer and matlab to freeze and crash without warning if you run it on big data sets. I remedied this situation by by putting the code in a wrapper that indexes smaller portions of the data to get the final large image which prevents memory overflows.</p>
  
  <p>I hope i made my mess of code clear enough. This code works fully well in matlab, but matlab is very terrible to be basing my projects in mainly because I can't give my code to other people. </p>
  
  <p>EDIT:</p>
  
  <p>This function is a vectorized program that basically does: (this is pseudocode so the indexing might not be right off the top of my head)</p>
  
  <p>It also does not have padding in this segment. The loading variable i useis a gaussian matrix or a lapacian that can range from 6x6 to 64x64. It can be any size really, as long as its smaller than the image.</p>
  
  <pre><code>    correlation_coeficcient_surface = function(Image,loading)
      [m,n] = size(image)
      [u1,u2] = size(loading)
      canvas = zeros(size(image))
      for yii = 1:n
           for xii = 1:m
                image_segment = Image(yii-floor(u1/2):yii+ceil(u1/2),xii-floor(u2/2):xii+ceil(u2/2));
                if(size(image_segment) == size(loading)
                    canvas(yii-floor(u1/2):yii+ceil(u1/2),xii-floor(u2/2):xii+ceil(u2/2)) = corr2(Image_segment,loading);
                end
            end
       end
  
  
      end
  </code></pre>
  
  <p>It got to be vectorized because iterating over every element made it take really long amounts of time on large images. </p>
  
  <p>Edit Edit:</p>
  
  <p>Here is what my filter actually does.</p>
  
  <p>This is a sample Image</p>
  
  <p><a href="http://i.imgur.com/o9kV3nK.png" rel="nofollow">http://i.imgur.com/o9kV3nK.png</a></p>
  
  <p>This is a sample loading shape that i use to correlate with</p>
  
  <p><a href="http://i.imgur.com/oYW3k2K.png" rel="nofollow">http://i.imgur.com/oYW3k2K.png</a></p>
  
  <p>this is after my matlab filter is finished the images are not aligned, i am just cropping examples to show you what it does shapewise.</p>
  
  <p><a href="http://i.imgur.com/aa4ljue.png" rel="nofollow">http://i.imgur.com/aa4ljue.png</a></p>
  
  <p>this is scipy.signal.convolve2d which does something that i am not intending.</p>
  
  <p><a href="http://i.imgur.com/vJhXMam.png" rel="nofollow">http://i.imgur.com/vJhXMam.png</a></p>