gpt4 book ai didi

python - 无法确定 NumPy 数组的切片索引

转载 作者:行者123 更新时间:2023-12-04 13:33:28 25 4
gpt4 key购买 nike

我有一个 X * Y 的 NumPy 数组元素,表示为扁平数组 ( arr = np.array(x * y) )。
给定以下值:

X = 832
Y = 961
我需要按以下顺序访问数组的元素:
arr[0:832:2]
arr[1:832:2]
arr[832:1664:2]
arr[833:1664:2]
...
arr[((Y-1) * X):(X * Y):2]
从数学上讲,我不确定如何实现 startstop对于循环中的每次迭代。

最佳答案

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)

关于python - 无法确定 NumPy 数组的切片索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63635610/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com