title: Converting MATLAB sub2ind function to Python
body: <p>I am converting this MATLAB code to Python. I am testing my class interpolation here to make sure the methods are working.
  However, I am getting this  error message</p>
  <blockquote>
  <p>not enough values to unpack (expected 2, got 0)***</p>
  </blockquote>
  <p>The MATLAB <code>reshape</code> function in Python is not exactly the same.</p>
  <p>I am also not sure about the nested function I created <code>sub2ind</code>.</p>
  <p>Original MATLAB code:</p>
  <pre class="lang-matlab prettyprint-override"><code>function [I3] = Ra_int(Im, x0, y0)
  [a1 b1]=size(x0);
  re_x0=reshape(x0,a1*b1,1);
  re_y0=reshape(y0,a1*b1,1);
  eIm1=Im(sub2ind(size(Im),floor(re_x0'+1),floor(re_y0'+1)));
  Im1=reshape(eIm1',size(Im));
  eIm2=Im(sub2ind(size(Im),floor(re_x0'+1),floor(re_y0')));
  Im2=reshape(eIm2',size(Im));
  eIm3=Im(sub2ind(size(Im),floor(re_x0'),floor(re_y0'+1)));
  Im3=reshape(eIm3',size(Im));`enter code here`
  eIm4=Im(sub2ind(size(Im),floor(re_x0'),floor(re_y0')));
  Im4=reshape(eIm4',size(Im));
  I3 = (x0 - floor(x0) ).* ((y0 - floor(y0) ).* Im1 + (floor(y0+1)-y0).* Im2) + (floor(x0+1) #- x0 ).* ((y0 - floor(y0) ).* Im3 + (floor(y0)+1-y0).* Im4);
  </code></pre>
  <p>My Python translation:</p>
  <pre class="lang-py prettyprint-override"><code>import numpy as np
  import math
  
  from PIL import Image
  
  class interpolation() :
      def __int__(self, x0, y0, Im):
          self.x0=x0
          self.y0=y0
          self.Im=Im
      
      
      def Ra_int(self,Im, x0, y0) :
          (a1 , b1)=  np.shape(x0)
          re_x0 = np.reshape(x0, a1@b1, 1); # 
          re_y0=np.reshape(y0,a1@b1,1); #same
          Im=Im.shape 
          def sub2ind(Im,re_x0,re_y0):
              return re_x0*Im[1] +re_y0
      
      
  ## Im1 reshaping
  eIm1 = Im(sub2ind(Im.shape, math.floor(re_x0.conj()+1),math.floor(re_y0.conj() + 1)));
  Im1 = np.reshape(eIm1.conj(),Im.shape); # not sure about using reshape
      
  ## Im2 reshaping
  eIm2 = Im(sub2ind(Im.shape, math.floor(re_x0.conj() + 1), math.floor(re_y0.conj())));
  Im2 = np.reshape(eIm2.conj(), Im.shape);  
      
  ## Im3 reshaping
  eIm3 = Im(sub2ind(Im.shape, math.floor(re_x0.conj()), math.floor(re_y0.conj() + 1)));
  Im3 = np.reshape(eIm3.conj(), Im.shape);  
  
  ## Im4 reshaping
  eIm4 = Im(sub2ind(Im.shape, math.floor(re_x0.conj()), math.floor(re_y0.conj())));
  Im4 = np.reshape(eIm4.conj(), Im.shape);  
  
  ### output
  I3 = (x0 - math.floor(x0) )* ((y0 - math.floor(y0) )* Im1 + (math.floor(y0+1)-y0)* Im2) + (math.floor(x0+1) - x0 )* ((y0 - math.floor(y0) )* Im3 +
                                                                                       (math.floor(y0)+1-y0)* Im4);
  
  ### print
  
  test=interpolation()
  print(test)
  print(test.Ra_int(30,40,40))
  </code></pre>