Better HN
topnewbestaskshowjobs
Log inSign up

oiuswv

2 karma · joined Nov 12, 2015 (11y ago) · 1 submissions

Submissions

comment 11y ago · link · parent

Here, I'll use logical indexing to pull out all the positive even numbers from this array of random integers:

Numpy:

  a = np.random.random_integers(low = -100, high = 100, size = (100,))
  a[np.logical_and(a % 2 == 0, a > 0)
(or a[(a % 2 == 0) * (a > 0)])

Logical indexing is used everywhere in numerical computing. Using functions like logical_and() or boolean multiplication or addition is more difficult to follow than Matlab or Julia.

Julia:

  a = rand(-100:100, 100)
  a[(a % 2 .== 0) & (a .> 0)]
Well, that's beautiful. Element-wise operations are prefixed with "."