我的输入是一个3x3x3的数组,例如:
m = [[[0, 1, 2], [3, 4, 5], [6, 7, 8]],
[[9, 10, 11], [12, 13, 14], [15, 16, 17]],
[[18, 19, 20], [21, 22, 23], [24, 25, 26]]]
这些是我想使用矢量或矩阵运算得到的输出:
out1 = [[0,1,2],[3,4,5],[6,7,8]] (corresponding to indice 0 along first axis)
out2 = [[0,1,2],[9,10,11],[18,19,20]] (corresponding to indice 0 along second axis)
out3 = [[0,3,6],[9,12,15],[18,21,24]] (corresponding to indice 0 along third axis)
我知道我可以使用numpy这样的python来做到这一点:
cube = np.arange(27).resize(3,3,3)
out1 = cube[0,:,:]
out2 = cube[:,0,:]
out3 = cube[:,:,0]
但是我需要在javascript中实现它。我知道如何使用循环和索引来做到这一点,但我认为可能会有更有效的方法。
答案 0 :(得分:1)
使用numjs
package that strives to transplant NumPy onto JS(特别是基础ndarray.pick
的scijs/ndarray
package):
var m = nj.array([[[0, 1, 2], [3, 4, 5], [6, 7, 8]],
[[9, 10, 11], [12, 13, 14], [15, 16, 17]],
[[18, 19, 20], [21, 22, 23], [24, 25, 26]]]);
console.log(m.pick(0,null,null));
<script src="https://cdn.jsdelivr.net/gh/nicolaspanel/numjs@0.15.1/dist/numjs.min.js"></script>
打印:
"[[0,1,2],[3,4,5],[6,7,8]]"