I want to convert the following Matlab code to the equivalent in Python:
M.*((S*U)./max(realmin, M*(U'*U)))
where:
S: n*n
M: n*m
U: n*m
I have done it by the following code:
x = (max(-sys.maxint, np.matmul(M, np.matmul(np.transpose(U), U))))
M = np.dot(M, ((np.matmul(S, U)) / x))
but I got the following error:
x = (max(-sys.maxint, np.matmul(M, np.matmul(np.transpose(U), U))))
ValueError: The truth value of an array with more than one element is
ambiguous. Use a.any() or a.all()
Would you please help me how I can convert the Matlab code to Python.
Matlab's max(a, b)
is a broadcasted elementwise maximum operation. Python's max(a, b)
isn't. Python's max
doesn't understand arrays, and you shouldn't use it on arrays.
For a broadcasted elementwise maximum, you want numpy.maximum
:
numpy.maximum(whatever, whatever)
Also, Matlab's realmin
is the smallest positive normalized double-precision floating-point number, while Python's sys.maxint
is a large negative int (and also nonexistent on Python 3). That's probably not what you want. The equivalent to Matlab's realmin
would be
sys.float_info.min
or
numpy.finfo(float).tiny
(numpy.finfo(float).min
is a different thing.)