I am converting matlab file to python code. my matlab file is :
function [q,len] = curve_to_q(p)
[n,N] = size(p);
for i = 1:n
v(i,:) = gradient(p(i,:),1/(N));
end
len = sum(sqrt(sum(v.*v)))/N;
v = v/len;
for i = 1:N
L(i) = sqrt(norm(v(:,i)));
if L(i) > 0.0001
q(:,i) = v(:,i)/L(i);
else
q(:,i) = v(:,i)*0.0001;
end
end
converted code is :
from __future__ import division
import numpy as np
from scipy.io import loadmat,savemat
import os
def curve_to_q(p):
n, N = p.shape # nargout=2
for i in range(1, (n +1)):
v[(i -1), :] = np.gradient(p[(i -1), :], 1 / (N))
len_ = np.sum(np.sqrt(np.sum(v.np.dot(v)))) / N
v = v / len_
for i in range(1, (N +1)):
L[(i -1)] = sqrt(norm(v[:, (i -1)]))
if L[(i -1)] > 0.0001:
q[:, (i -1)] = v[:, (i -1)] / L[(i -1)]
else:
q[:, (i -1)] = v[:, (i -1)] * 0.0001
return q, len_
But, there seems to be problem in
len_ = np.sum(np.sqrt(np.sum(v.np.dot(v)))) / N
and
L[(i -1)] = sqrt(norm(v[:, (i -1)]))
how can i make it proper conversion to python?
vanywhere incurve_to_q, which could be causing problems. Same forL. Ifvis an array, you should be using*, notnp.dot, and ifvis a matrix, you should be usingnp.multiply(because you want.*, which is elementwise multiplication, not matrix multiplication).v.np.dottov.dot