2017-04-15 69 views
1

我有一个有点简单的事,但我仍然没有成功与numpy的MGRID & meshgrid。 我有100种元素的numpy的矢量:如何使用numpy的MGRID或meshgrid对于简单的任务

[0,0,0...0] 

,我希望创建一个1000x100 numpy的阵列那样的,每次由0.1增加矢量值中的一个,切换到下一个矢量值达到1.0时。 所以,第一个迭代应该给我:

[0.1 0 0..0] 
[0.2 0 0..0] 
. 
. 
[0.9 0 0..0] 
[1.0 0 0..0] 

从现在开始我要重复的第二向量数量,保持以前的值:

[1.0 0.1 0 0..0] 
[1.0 0.2 0 0..0] 
[1.0 0.3 0 0..0] 

等。最后的矩阵应该是这样的1000x100,但我并不需要得到所有的值一起在一个大numpy的阵列 - 这将是足够的迭代,并在每次迭代的corrisponding矢量产生。 在此先感谢!

回答

1

这里有一个方法使用initializationnp.maximum.accumulate -

def create_stepped_cols(n): # n = number of cols 
    out = np.zeros((n,10,n)) 
    r = np.linspace(0.1,1.0,10) 
    d = np.arange(n) 
    out[d,:,d] = r 
    out.shape = (-1,n) 
    np.maximum.accumulate(out, axis=0, out = out) 
    return out 

样品试验 -

In [140]: create_stepped_cols(3) 
Out[140]: 
array([[ 0.1, 0. , 0. ], 
     [ 0.2, 0. , 0. ], 
     [ 0.3, 0. , 0. ], 
     [ 0.4, 0. , 0. ], 
     [ 0.5, 0. , 0. ], 
     [ 0.6, 0. , 0. ], 
     [ 0.7, 0. , 0. ], 
     [ 0.8, 0. , 0. ], 
     [ 0.9, 0. , 0. ], 
     [ 1. , 0. , 0. ], 
     [ 1. , 0.1, 0. ], 
     [ 1. , 0.2, 0. ], 
     [ 1. , 0.3, 0. ], 
     [ 1. , 0.4, 0. ], 
     [ 1. , 0.5, 0. ], 
     [ 1. , 0.6, 0. ], 
     [ 1. , 0.7, 0. ], 
     [ 1. , 0.8, 0. ], 
     [ 1. , 0.9, 0. ], 
     [ 1. , 1. , 0. ], 
     [ 1. , 1. , 0.1], 
     [ 1. , 1. , 0.2], 
     [ 1. , 1. , 0.3], 
     [ 1. , 1. , 0.4], 
     [ 1. , 1. , 0.5], 
     [ 1. , 1. , 0.6], 
     [ 1. , 1. , 0.7], 
     [ 1. , 1. , 0.8], 
     [ 1. , 1. , 0.9], 
     [ 1. , 1. , 1. ]]) 

In [141]: create_stepped_cols(100).shape 
Out[141]: (1000, 100) 
+0

它就像一个魅力,非常感谢! –