Octave(or Matlab) sub2ind function convert to python code

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Octave(or Matlab) sub2ind function convert to python code



I implement Matlab 'strel' function to python language.



I have some difficulty converting to python code.



plz let me know how to convert this function.



I think numpy's ravel_multi_index function equal to sub2ind.



but it dose not work well.



:(



Octave code


degrees = 30
linelen = 5


deg90 = mod (degrees, 90);
if (deg90 > 45)
alpha = pi * (90 - deg90) / 180;
else
alpha = pi * deg90 / 180;
endif
ray = (linelen - 1)/2;

c = round (ray * cos (alpha)) + 1;
r = round (ray * sin (alpha)) + 1;


line = false (r, c);

m = tan (alpha);
x = [1:c];

y = r - fix (m .* (x - 0.5));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
indexes = sub2ind ([r c], y, x);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



and My python code here.


def strel_line(length,degree):
import numpy as np
from numpy import pi,cos,sin,tan,fix

deg90 = degree % 90
if deg90 > 45:
alpha = pi * (90 - deg90) / 180
else:
alpha = pi * deg90 / 180

center = (length -1) /2

c =int(round (center * cos(alpha)) + 1)
r =int(round (center * sin(alpha)) + 1)

line = np.zeros((r,c),dtype = 'int')
print(line)
m = tan(alpha)
x = np.int64( np.arange(1,c+1) )
y = np.int64(r - fix(np.multiply(m,x-0.5)) )
temp= np.array([r,c])









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

C++ virtual function: Base class function is called instead of derived