如何实现 numpy.repeat 函数的效果

如何实现类似于 numpy.repeate 的功能,可以将向量按照不同维度进行复制?
attachments-2023-05-Fz9IYp1564759149883d8.png

请先 登录 后评论

1 个回答

veryOrdinary

上述图中的按列复制,可以通过 SQL 语句实现,如有一个表 t = table(1 2 3 as a), 则通过 select a, a as b, a as c from t 即可实现上述效果。或者定义为矩阵 m = matrix(1 2 3) 然后使用矩阵复制函数 repmat(m, 1, 3) 实现同样的效果。

其它 repeat 的实现可以参考下述代码改写:

# python
x = np.array([[1,2],[3,4]])
>>> np.repeat(x, 2)
array([1, 1, 2, 2, 3, 3, 4, 4])

# ddb
x = [[1,2], [3,4]]
tmp = x.flatten()
tmp.stretch(2 * tmp.count())

----------------
# python
>>>
np.repeat(x, 3, axis=1)
array([[1, 1, 1, 2, 2, 2],
       [3, 3, 3, 4, 4, 4]])

# ddb
x = [[1,2], [3,4]]
len = each(count, x)
repeats=3
loop(stretch, x, len*repeats)

-------------------
# python
>>> np.repeat(x, [1, 2], axis=0)
array([[1, 2],
       [3, 4],
       [3, 4]])

# ddb
t = table([1,2] as a, [3, 4] as b)
select a, b, b as c from t




请先 登录 后评论
  • 1 关注
  • 0 收藏,619 浏览
  • Polly 提出于 2023-05-30 14:02

相似问题