Apply sin to even indices in the array; 2i - question about the code

Hi,

I’m confused about something, especially a portion of the code.

Let’s say:

dims = np.arange(8)[np.newaxis, :]

It gives array([[0, 1, 2, 3, 4, 5, 6, 7]])

If I do np.sin(dims[:, 0::2]) ,

I get array([[ 0. , 0.90929743, -0.7568025 , -0.2794155 ]]) which compute the sinus for only even indices (so array([[0, 2, 4, 6]]))

So, why when I’m doing dims[:, 0::2] = np.sin(dims[:, 0::2])

I get dims= array([[0, 1, 0, 3, 0, 5, 0, 7]]) and especially I get 0 instead of the sinus value ?

Did I miss something ?

Thank you

Your array dims is “int”. And, the output from “sin” should be “float”, and each value is less than 1.
So, all elements become “0” as “int” and are inserted to dims.

If you want to insert “float” values into “int” array, you just need to convert dims to a float type like
dims = dims.astype(float)

1 Like