2

Is there an elegant way (without loops) to do this kind of assignment with numpy indexing?

import numpy as np

m = 3
n = 5
small_matrix = np.random.randn(m, n)
big_matrix = np.zeros((100, 100))
row_indices = [3, 16, 9]
col_indices = [31, 45, 69, 71, 83]

for i in range(m):
    for j in range(n):
        big_matrix[row_indices[i], col_indices[j]] = small_matrix[i, j]
3
  • 3
    There are lots of almost-duplicates: np.ix_ is the way to go, i.e. big_matrix[np.ix_(row_indices, column_indices)] = small_matrix. Commented Sep 16, 2018 at 20:31
  • @DSM Thanks! I didn't find those. Commented Sep 16, 2018 at 20:42
  • I cheated because I knew what function to look for. ;-) Commented Sep 16, 2018 at 20:45

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.