ruby-numo / numo-narray

Ruby/Numo::NArray - New NArray class library
http://ruby-numo.github.io/narray/
BSD 3-Clause "New" or "Revised" License
413 stars 41 forks source link

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

Open kojix2 opened 3 years ago

kojix2 commented 3 years ago

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]]