Skip to content

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

Open

Description

@kojix2

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions