2D Numpy Array初学者遇到麻烦

时间:2013-04-05 04:09:59

标签: python arrays numpy multidimensional-array

我的问题是尝试使用数组,每个元素都是2个值的元组。

具体地说,问题是产生200的随机二维步行(但是用于测试说2)具有最大距离的步骤,然后每阶段步行100次(仅尝试2次),并且以最大的每个阶段开始与前一阶段的起源距离。

我可以成功生成随机步骤数组并让它们返回最终位置(x,y)值,并计算它们与每次步行原点的距离:

在这些功能中定义:

#............................................................getPositionInteger
def getPosInt(description) :
    """Asks the user to enter a positive integer"""
    askAgain = False
    while askAgain == False:
        try:
            posInt = eval(raw_input("\n %s :  " %description))
            assert 0 < posInt , "Not a positive integer"
            assert type(posInt) == int , "Not a positive integer"
            askAgain = True
        except :
            print "Input failed, not a positive integer"
    return posInt
#...............................................................initialPosition
def initialPosition() :
    """Initial position of walker at the start of a random walk"""
    return (0.0, 0.0)

#......................................................................distance
def distance(posA, posB) :
    """Distance between two positions"""
    xi = posA[0] ; yi = posA[1]
    xf = posB[0] ; yf = posB[1]
    return np.sqrt((xf-xi)**2+(yf-yi)**2)

    #..................................................................getPositions
def getPositions(start, nSteps, maxStep):
    xArray = maxStep * np.random.random(nSteps+1)* np.cos(2.0 * np.pi * random.random())
    yArray = maxStep * np.random.random(nSteps+1)* np.sin(2.0 * np.pi * random.random())
    xArray[0] = start[0]
    yArray[0] = start[-1]
    xArray = np.cumsum(xArray)
    yArray = np.cumsum(yArray)                                                    
    return (xArray[-1], yArray[-1])

但我无法获得每个阶段每个步行的最终位置数组(x,y)形式每个阶段

这是主要的脚本,我遇到了麻烦:

import numpy as np
import matplotlib.pylab as plt
import random
import time

MAX_STEP_SIZE = 0.90    # maximum size of a single step [m]
random.seed(12345)

#..........................................................................main
def main ():
    ''''''
    print "RANDOM WALK IN STAGES IN TWO DIMENSIONS"
    userdata = getDetails()
    print "\nPlease wait while random walks are generated and analyzed..."
    NUM_STAGES = userdata[0]
    NUM_WALKS = userdata[1]
    NUM_STEPS = userdata[2]
    stageStart = initialPosition()
    for stage in np.arange(NUM_STAGES):
        walks = np.zeros((NUM_WALKS, NUM_WALKS), dtype=np.ndarray)
        for walk in walks:
            walk = getPositions(stageStart, NUM_STEPS, MAX_STEP_SIZE)
            print walk
        print walks

您将看到我在制作(x,y)样式数组时遇到问题,其中[0 0]应为[0.0,0.0]并且打印两次,此外,它不会更改为最终位置。

我非常感谢您提供帮助,建议或参考。 谢谢 -sid

0 个答案:

没有答案