生成模式化的numpy矩阵 [英] generate a patterned numpy matrix

查看:93
本文介绍了生成模式化的numpy矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在其余矩阵为"0"的情况下生成圆形图案为"1"的numpy矩阵?所以基本上是

Is it possible to generate a numpy matrix with a circular pattern of "1"s in a rest matrix of "0"s? So basically a

generate(ysize, xsize, ycenter, xcenter, radius)

应该看起来像

[000000000]
[000000000]
[000001000]
[000011100]
[000111110]
[000011100]
[000001000]
[000000000]

(好吧,这看起来很愚蠢,但是在1000x1000的比例上还是有道理的)

(ok this looks stupid but on a 1000x1000 scale it would make sense)

numpy中有这种可能性吗?

Is there such a possibility in numpy?

推荐答案

def generate(ysize, xsize, ycenter, xcenter, radius):
    x = np.arange(xsize)[None,:]
    y = np.arange(ysize)[:,None]
    return ((xcenter - x) ** 2 + (ycenter - y) ** 2 <= radius ** 2) * 1


generate(10,8,4,3,2)


array([[0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 1, 0, 0, 0, 0],
       [0, 0, 1, 1, 1, 0, 0, 0],
       [0, 1, 1, 1, 1, 1, 0, 0],
       [0, 0, 1, 1, 1, 0, 0, 0],
       [0, 0, 0, 1, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0]])

这篇关于生成模式化的numpy矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆