从满足条件的NumPy矩阵的每一行中取N个第一个值

时间:2018-01-22 11:05:12

标签: python arrays numpy matrix mask

我有一个// main.cpp // gloabl vars NonStd::Sphere sun = NonStd::Sphere ( 10 ); NonStd::Sphere earth = NonStd::Sphere ( 3 ); NonStd::Sphere moon = NonStd::Sphere ( 1 ); NonStd::Object space = NonStd::Object (); void render (); void modelInit (); void idle (); void windowOnChange ( int width, int height ); void mouseOnDrag ( int x, int y ); int main ( int args_len, char ** args_context ) { glutInit ( &args_len, args_context ); glutInitDisplayMode ( GLUT_SINGLE ); glutInitWindowSize ( WINDOW_WIDTH, WINDOW_HEIGHT ); glutInitWindowPosition ( 100, 100 ); glutCreateWindow ( "Solar System Simulation" ); glEnable ( GL_NORMALIZE ); glEnable ( GL_COLOR_MATERIAL ); // all models initialization modelInit (); // event handlers glutDisplayFunc ( render ); glutReshapeFunc ( windowOnChange ); // glutMotionFunc ( mouseOnDrag ); // global idle func glutIdleFunc ( idle ); glutMainLoop (); return 0; }; void render () { glClearColor ( .2, .3, .5, .8 ); glClear ( GL_COLOR_BUFFER_BIT ); if ( sun.isObjectShown () ) { sun.draw (); } if ( earth.isObjectShown () ) { earth.draw (); } if ( moon.isObjectShown () ) { moon.draw (); } glFlush (); }; void modelInit () { // object visibility default is false sun.setVisible ( true ); // move to proper position to for object for better viewing sun.translateZ ( -90.0 ); // set object texture sun.setTexture ( "resources/earth.jpg", 100, 100 ); // spin default is false, toggle it for spinning sun.toggleSpin (); earth.setVisible ( true ); earth.translateZ ( -90.0 ); earth.translateX ( 26.0 ); earth.setTexture ( "resources/earth.jpg", 100, 100 ); earth.toggleSpin (); earth.setSpinSpeed ( 2 ); moon.setVisible ( true ); moon.translateZ ( -90.0 ); moon.translateX ( 20.0 ); moon.setTexture ( "resources/earth.jpg", 100, 100 ); moon.toggleSpin (); }; After I set the texture on my sphere object, the sphere turn into this yellow color and before setting the texture, it was white, does this mean the texture already set but I have not yet specify the texture coordinate for it ? 和一个numpy vector

我需要从矩阵中的每一行获取小于(或等于)向量中相应行的第一个N(比如3)值。

所以如果这是我的载体:

numpy array

这是我的矩阵:

7,
9,
22,
38,
6,
15

输出应为:

[[ 20.,   9.,   7.,   5.,   None,   None],
 [ 33.,  21.,  18.,   9.,   8.,   7.],
 [ 31.,  21.,  13.,  12.,   4.,   0.],
 [ 36.,  18.,  11.,   7.,   7.,   2.],
 [ 20.,  14.,  10.,   6.,   6.,   3.],
 [ 14.,  14.,  13.,  11.,   5.,   5.]]

是否有任何有效的方法可以使用蒙版或其他东西,没有丑陋的[[7,5,None], [9,8,7], [21,13,12], [36,18,11], [6,6,3], 14,14,13]] 循环?

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:3)

方法#1

此处有一个broadcasting -

def takeN_le_per_row_broadcasting(a, b, N=3): # a, b : 1D, 2D arrays respectively
    # First col indices in each row of b with <= corresponding one in a
    idx = (b <= a[:,None]).argmax(1)

    # Get all N ranged column indices
    all_idx = idx[:,None] + np.arange(N)

    # Finally advanced-index with those indices into b for desired output
    return b[np.arange(len(all_idx))[:,None], all_idx]

方法#2

NumPy Fancy Indexing - Crop different ROIs from different channels's solution的启发,我们可以利用np.lib.stride_tricks.as_strided进行有效的补丁提取,就像这样 -

from skimage.util.shape import view_as_windows

def takeN_le_per_row_strides(a, b, N=3): # a, b : 1D, 2D arrays respectively
    # First col indices in each row of b with <= corresponding one in a
    idx = (b <= a[:,None]).argmax(1)

    # Get 1D sliding windows for each element off data
    w = view_as_windows(b, (1,N))[:,:,0]

    # Use fancy/advanced indexing to select the required ones
    return w[np.arange(len(idx)), idx]