如何将numpy数组值复制到更高维度 [英] how to copy numpy array value into higher dimensions
本文介绍了如何将numpy数组值复制到更高维度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在2d中有一个(w,h)np数组.我想制作一个值大于1的3d尺寸,然后将其值沿第3个尺寸复制.我希望广播能做到,但不能做到.这就是我的做法
I have a (w,h) np array in 2d. I want to make a 3d dimension that has a value greater than 1 and copy its value over along the 3rd dimensions. I was hoping broadcast would do it but it can't. This is how i'm doing it
arr = np.expand_dims(arr, axis=2)
arr = np.concatenate((arr,arr,arr), axis=2)
有更快的方法吗?
推荐答案
您可以推动向前所有暗淡,引入单例暗淡/新轴作为最后一个暗淡以创建3D
数组,并然后使用 np.repeat
重复三遍,就像这样-
You can push all dims forward, introducing a singleton dim/new axis as the last dim to create a 3D
array and then repeat three times along that one with np.repeat
, like so -
arr3D = np.repeat(arr[...,None],3,axis=2)
这是使用 np.tile
的另一种方法-
Here's another approach using np.tile
-
arr3D = np.tile(arr[...,None],3)
这篇关于如何将numpy数组值复制到更高维度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文