LoginSignup
0
0

More than 5 years have passed since last update.

np.arrayで各行それぞれの好きな列に代入する

Posted at

こういう細かいやり方がいちいちわからん

>>> import numpy as np
>>> A = np.zeros((4,2))
>>> A
array([[ 0.,  0.],
       [ 0.,  0.],
       [ 0.,  0.],
       [ 0.,  0.]])
>>> index = np.array([0,1,0,1])
>>> index
array([0, 1, 0, 1])
>>> values = np.array([10,20,10,10])
>>> values
array([10, 20, 10, 10])
>>> A[np.arange(4),index] = values
>>> A
array([[ 10.,   0.],
       [  0.,  20.],
       [ 10.,   0.],
       [  0.,  10.]])
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0