0

I want to make a square signal (as an array). I am trying the following code:

import numpy as np

x = np.linspace(0,2000, 100)

def func(x):
    x = np.where(x<500 or x>530, 0, 2)
    return x

y = func(x)

unfortunately, it throws the following error:

Traceback (most recent call last):
  File "test.py", line 24, in <module>
    y = func(x)
  File "test.py", line 20, in func
    x = np.where(x<500. or x>530, 0., 2.)
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

I do not understand what is wrong with my code.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
user8224662
  • 131
  • 2
  • 6
  • 13

1 Answers1

0

You can not use or here. or is a Python function that evaluates the truthiness of the operands and will, based on that return one of the operands.

You can use the logical or (|) however, which is implemented by numpy itself:

x = np.where((x < 500) | (x > 530), 0, 2)
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555