0

I am trying to get an array containing every other element in my original array a. In python, I can do this with a[::2]. I wonder in what way I can do this simply and fast in DolphinDB.

I can write a for loop and repeatedly append! the every other element. But I don't think it's an elegant solution.

a = 1..10
result = []
skip = false
for (element in a) {
    if (!skip)
        result.append!(element)
    skip = !skip
}
Clyx
  • 43
  • 7

1 Answers1

0

create an index vector to retrieve every other element of the original vector.

a[0..((a.size() - 1)/2) * 2]
Davis Zhou
  • 353
  • 4
  • 6