I am working on a matlab conversion code. what is equivalent of .* in matlab with python?
len = sum(sqrt(sum(v.*v)))/N;
where v is numpy array :
v = [array([-35289.38919481, -30575.56015338, -21456.41798462, ...,
19796.17331542, 11216.34277023, 6977.87432284])]
N = 18225
In such cases, how will I convert code to python?
For numpy arrays, just using * will do the element-wise multiplication as in Matlab's .*
http://wiki.scipy.org/NumPy_for_Matlab_Users
you can use,
len = numpy.sum(numpy.sqrt(numpy.sum(v[0] * v[0], axis=0))) / N
Note: If you want to use matrices instead of arrays in numpy, you have to use the multiply
method.
v_squared_sum = v.dot(v.transpose())
len_ = np.sum(np.sqrt(v_squared_sum)) / N
import numpy as np
v = np.array([1,2,3])
N = 3
len_ = np.sqrt(np.dot(v, v)) / N
print(len_)
Result:
1.24721912892
Do not use len
as a variable name, because this shadows the built-in length function len()
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.