Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matlab single() function in numpy?

Tags:

python

numpy

I try to convert matlab code to python/numpy code.

I have this line:

l = single(l)

"l" is a array of arrays and as the matlab docu says "Convert to single precision".

How can I do that with numpy?

like image 755
gustavgans Avatar asked Dec 06 '25 09:12

gustavgans


1 Answers

To convert a two-dimensional numpy array to single-precision, use astype and give it the float32 argument. For example:

>>> import numpy as np
>>> a = np.array([[1.], [2.], [3.]])
>>> a
array([[ 1.],
       [ 2.],
       [ 3.]])
>>> a = a.astype('float32')
>>> a
array([[ 1.],
       [ 2.],
       [ 3.]], dtype=float32)

For more about numeric and array data types, see the documentation.

like image 133
user4815162342 Avatar answered Dec 07 '25 21:12

user4815162342



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!