繁体   English   中英

在numpy数组中建立索引

[英]Indexing in numpy array

给定一个查询表:

colors = [  [0,0,0],\
            [0, 255, 0],\
            [0, 0, 255],\
            [255, 0, 0]]

索引numpy矩阵输入2x2:

a = np.array([[0,1],[1,1]])

如何将a映射到2x2x3矩阵b,其中b[i][j] = colors[a[i][j]] 我想避免在这里使用for循环。

你有没有尝试过:

colors[a]

这是一个完整的示例:

import numpy as np

colors = np.array([[0,0,0],
                   [0, 255, 0],
                   [0, 0, 255],
                   [255, 0, 0]
                  ])
a = np.array([[0, 1], [1, 1]])

new = colors[a]
new.shape
# (2, 2, 3)
new
# array([[[  0,   0,   0],
#         [  0, 255,   0]],
#
#        [[  0, 255,   0],
#         [  0, 255,   0]]])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM