Python 如何在numpy.zeros中查找最后一个位置

Python 如何在numpy.zeros中查找最后一个位置,python,numpy,Python,Numpy,对编程非常陌生,对不起,我不知道我的问题是否有意义 这是我的密码: import numpy import pylab import random L=[] n =int(input("number of steps :")) x = numpy.zeros(n) y = numpy.zeros(n) for i in range(1, n): Direction = random.randint(1, 4) if Direction

对编程非常陌生,对不起,我不知道我的问题是否有意义

这是我的密码:

import numpy 
import pylab 
import random 
L=[]
n =int(input("number of steps :"))
  
x = numpy.zeros(n) 
y = numpy.zeros(n) 
 
for i in range(1, n): 
    Direction = random.randint(1, 4) 
    if Direction == 1: 
        x[i] = x[i - 1] + 1
        y[i] = y[i - 1] 
    elif Direction == 2: 
        x[i] = x[i - 1] - 1
        y[i] = y[i - 1] 
    elif Direction == 3: 
        x[i] = x[i - 1] 
        y[i] = y[i - 1] + 1
    else: 
        x[i] = x[i - 1] 
        y[i] = y[i - 1] - 1
    L.append(Direction)
print("last direction was :",x ,y)
      

pylab.title("Walking)") 
pylab.plot(x, y) 
pylab.savefig("walk.png") 
pylab.show() 
它基本上是一个像素在x轴和y轴上的二维运动N次,我试图找到它的最后一个位置。我通过打印(x,y)并记下它给我的最后一个数字来实现这一点,但试图找到更好的解决方案。。有什么想法吗


谢谢

这里有另一种思考方法。不要循环和添加,只需跟踪方向向量即可。在您的示例中,您有四种可能的旅行方式:

directions = np.array([[1, 0], [-1, 0], [0, 1], [0, -1]])
可能代表东、西、北、南

您可以通过多种方式生成这些向量的随机列表,一种简单易懂的方法是创建一个索引的随机列表,并使用这些索引选择前进的方向。例如:

indices = np.random.randint(4, size=n)
# just a list of ints like array([2, 3, 1, 1, 3])

direction_vectors = directions[indices]
# a list of vectors
通过上述索引,您可以得到如下向量列表:

array([[ 0,  1],
       [ 0, -1],
       [-1,  0],
       [-1,  0],
       [ 0, -1]])
向量的好处在于,你可以把它们相加,得到最终的位置。使用numpy,只需调用
sum()

这将为您提供最终位置:
数组([-2,-1])
。您总共在
x
方向行驶了
-2
,在
y
方向行驶了
-1

在所有这些方面,可能看起来像:

n = 5
directions = np.array([[1, 0], [-1, 0], [0, 1], [0, -1]])

direction_vectors = directions[np.random.randint(len(directions), size=n)]
direction_vectors.sum(axis=0)
如果您想从原点以外的地方开始,只需添加该点即可

如果需要给定方向向量的每一步的当前点,可以使用以下公式计算累积和:

查找最后一个累积和与查找上面的和相同,因此您可以索引最终位置:

positions = direction_vectors.cumsum(axis=0)
positions[-1]
# array([-2, -1])

这是另一种思考的方式。不要循环和添加,只需跟踪方向向量即可。在您的示例中,您有四种可能的旅行方式:

directions = np.array([[1, 0], [-1, 0], [0, 1], [0, -1]])
可能代表东、西、北、南

您可以通过多种方式生成这些向量的随机列表,一种简单易懂的方法是创建一个索引的随机列表,并使用这些索引选择前进的方向。例如:

indices = np.random.randint(4, size=n)
# just a list of ints like array([2, 3, 1, 1, 3])

direction_vectors = directions[indices]
# a list of vectors
通过上述索引,您可以得到如下向量列表:

array([[ 0,  1],
       [ 0, -1],
       [-1,  0],
       [-1,  0],
       [ 0, -1]])
向量的好处在于,你可以把它们相加,得到最终的位置。使用numpy,只需调用
sum()

这将为您提供最终位置:
数组([-2,-1])
。您总共在
x
方向行驶了
-2
,在
y
方向行驶了
-1

在所有这些方面,可能看起来像:

n = 5
directions = np.array([[1, 0], [-1, 0], [0, 1], [0, -1]])

direction_vectors = directions[np.random.randint(len(directions), size=n)]
direction_vectors.sum(axis=0)
如果您想从原点以外的地方开始,只需添加该点即可

如果需要给定方向向量的每一步的当前点,可以使用以下公式计算累积和:

查找最后一个累积和与查找上面的和相同,因此您可以索引最终位置:

positions = direction_vectors.cumsum(axis=0)
positions[-1]
# array([-2, -1])

numpy数组的最后一个值可以像Python中的列表等一样使用
-1
进行索引。因此,可以使用
x[-1]
y[-1]
检索最终位置。numpy数组的最后一个值可以像Python中使用
-1
的列表等一样进行索引。因此,可以使用
x[-1]
y[-1]
检索最终位置,谢谢!!这就是我需要的谢谢!!这就是我需要的