numo-narray icon indicating copy to clipboard operation
numo-narray copied to clipboard

How to specify the index of each dimension as an array and perform assignments

Open kojix2 opened this issue 3 years ago • 0 comments

In python's numpy, you can substitute by specifying the indices of the x and y axes.

import numpy as np

a = np.arange(9).reshape(3,3)
x = [0, 1, 2]
y = [2, 0, 1]
c = [1000, 1001, 1002]
a[x, y] = c

a
# array([[   0,    1, 1000],
#        [1001,    4,    5],
#        [   6, 1002,    8]])

The same thing is not possible in Ruby. What should I do?

a = Numo::Int32.new(3,3).seq
# [[0, 1, 2], 
#  [3, 0, 5], 
#  [6, 7, 0]]

x = [0, 1, 2]
y = [2, 0, 1]
c = [1000, 1001, 1002]

a[x, y] = c

# Numo::Int32#shape=[3,3]
# [[0, 0, 1000], 
#  [0, 0, 1001], 
#  [0, 0, 1002]]

kojix2 avatar Jul 04 '21 18:07 kojix2