maya
maya

Reputation: 29

index 22 is out of bounds for axis 0 with size 2

I am trying to convert the following matlab code to pyhton and want to print em1. Howvever I am getting the following traceback error. When I tried to print I_1 , it works but when I tried to print em1, the code shows an error

Upvotes: 0

Views: 99

Answers (1)

Jeremy
Jeremy

Reputation: 166

Main problems are:

  • Im test array isn't big enough for the test indices
  • Need to call Im.flat to access via index

Here is some example code that should help clear things up more elegantly:

import numpy as np

x0 = np.array([[5, 6], [3, 5]])
y0 = np.array([[9, 10], [4, 1]])

Im = np.random.normal(0, 1, (36, 36))

I_1 = np.ravel_multi_index(
    ((x0.flatten() + 1).astype(np.int64),
     (y0.flatten() + 1).astype(np.int64),)
    , dims=Im.shape, order='F')

em1 = Im.flat[I_1]

Upvotes: 0

Related Questions