1

I have a NumPy array of X * Y elements, represented as a flatted array (arr = np.array(x * y)).

Given the following values:

X = 832
Y = 961

I need to access elements of the array in the following sequence:

arr[0:832:2]
arr[1:832:2]
arr[832:1664:2]
arr[833:1664:2]
...
arr[((Y-1) * X):(X * Y):2]

I'm not sure, mathematically, how to achieve the start and stop for each iteration in a loop.

5
  • This is an x-y problem. You almost never need to loop through a numpy array directly. If you show what you are trying to do with each chunk, I can improve my answer. Commented Aug 28, 2020 at 15:53
  • @MadPhysicist I'm not looping the array, I'm looping a set of X and Y values and inserting values at the given [start:stop:step], such as arr[1:832:2] = 4. Commented Aug 28, 2020 at 15:55
  • Can you show what you are actually doing? I still think this is an x-y problem because your goal is to access the elements of the array. For what purpose? Commented Aug 28, 2020 at 15:57
  • @MadPhysicist Script is on an entirely different machine (separated networks) so I can't copy and paste, would rather not introduce typos. That said, arr is a structured array representing a hexagon tessellation. arr has a shape of (X * Y, ) and each element has a shape of (7, 2). I'm iterating over the Y (column) range to assign values to the tessellation since each column shares x-axis values. Commented Aug 28, 2020 at 16:01
  • Take a look at my answer to see if it helps Commented Aug 28, 2020 at 18:16

3 Answers 3

1
This should do the trick

Y = 961
X = 832

all_ = np.random.rand(832*961)

# Iterating over the values of y
for i in range(1,Y):
    # getting the indicies from the array we need
    # i - 1 = Start
    # X*i = END
    # 2 is the step
    indicies = list(range(i-1,X*i,2))
    # np.take slice values from the array or get values corresponding to the list of indicies we prepared above
    required_array = np.take(indices=indices)
    
Sign up to request clarification or add additional context in comments.

Comments

0

To anybody interested in this solution (per iteration, not incrementing the shift each iteration):

for i in range(Y):
    shift = X * (i // 2)
    begin = (i % 2) + shift
    end = X + shift
    print(f'{begin}:{end}:2')

3 Comments

Use Y*2 in the for loop. When you use Y it only runs till 399360
@RamHarnish Why? I should only need to iterate over Y.
According to your question, the last term should be (Y-1) * X), (X * Y):2 which is 798720: 799552 but your loop ends with 399360:400192:2
0

Let's say you have an array of shape (x * y,) that you want to process in chunks of x. You can simply reshape your array to shape (y, x) and process the rows:

>>> x = 832
>>> y = 961
>>> arr = np.arange(x * y)

Now reshape, and process in bulk. In the following example, I take the mean of each row. You can apply whatever functions you want to the entire array this way:

>>> arr = arr.reshape(y, x)
>>> np.mean(arr[:, ::2], axis=1)
>>> np.mean(arr[:, 1::2], axis=1)

The reshape operation does not alter the data in your array. The buffer it points to is the same as the original. You can invert the reshape by calling ravel on the array.

Comments

Your Answer

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