-2

I have such an array:

    x = np.array([[1,2], [3,4], [5,6]]) 

I want to find elements bigger than 3. I am trying:

     ppoc = np.zeros((3,3))

     ixu = np.argwhere(x > 2)
     ppoc = ppoc[0, ixu]

But the problem is ppoc is a 2*2 array, but I need to return an array with the same size as x, which the rest of the elements are zero.

ppoc must look like:

ppoc = [[0,0], [3,4], [5,6]]

Does anyone have an idea how to do that?

  • You never defined `ppoc` in the first place... And what exactly is your expected output? – Lecdi Jun 24 '22 at 17:52
  • It's not really clear what you are trying to do. It would help if you explicitly told us what `ppoc` is and what your expected result is. – Mark Jun 24 '22 at 17:52
  • You can't maintain multidimensionality when filtering an array because you cannot guarantee that the array will not become ragged. Best you can do is return a boolean array where that condition is true. As soon as you use that mask to select the items though, you lose the dimensionality. Note that `np.argwhere` returns a tuple of index arrays, each corresponding to the original array's axes. – ddejohn Jun 24 '22 at 17:54
  • Please read about how to provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – ddejohn Jun 24 '22 at 18:00
  • Your question is very unclear, but to get all value greater than 3 `print("Values bigger than 10 =", x[x>3])` – Sachhya Jun 24 '22 at 18:01

2 Answers2

2

You can vectorize the computation that sends t to 0 or t depending on if t < 3, then apply this vectorized function to x:

np.vectorize(lambda t: 0 if t < 3 else t)(x)

this evaluates to:

array([[0, 0],
       [3, 4],
       [5, 6]])
John Coleman
  • 51,337
  • 7
  • 54
  • 119
0

If i understood correctly, you want to replace every element in the array by 0 if it's smaller than 3. Here's the answer :

x = np.array([[1,2], [3,4], [5,6]])
ppoc = x * (x > 2)

Output :
array([[0, 0],
   [3, 4],
   [5, 6]])

Is this what you wanted ?

FlippingTook
  • 151
  • 1
  • 7