0

everyone. I want to create a random array with a size of 1x1000 and it will have random integers from 0 to 3. Then I want to assign those numbers in different locations in a 2-D cartesian plane. I use real and imaginary axis as my x and y axis. When I try to assign it to 1j or -1j I have an error message:

TypeError("can't convert complex to float")

Can you help me please?

import numpy as np
from math import *

M = 4
N = 1000

firstsymbols = np.random.randint(M, size=N)
symbols = np.zeros(N)

for i in range(1,N):
    if (firstsymbols[i] == 0):
       symbols[i] = 1
    elif (firstsymbols[i] == 1):
       symbols[i] = 1j
    elif (firstsymbols[i] == 2):
       symbols[i] = -1
    else:
       symbols[i] = -1j

print(symbols)
2
  • 1
    The problem is you have an NumPy array of dtype float; you're trying to assign a complex number into a "slot" that can only take floats. Commented Aug 17, 2018 at 14:45
  • 1
    All you need to do is use a complex rather that float dtype when creating your array with zeros. Commented Aug 17, 2018 at 16:00

1 Answer 1

1

You can create a numpy array of complex numbers with broadcasting,

1j * np.arange(10)

Or you can keep a 2D array where the first column is the real part and the second column is the imaginary part.

Sign up to request clarification or add additional context in comments.

1 Comment

I divided symbols into real and imaginary matrices. It works when I combine them

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.