Jun-20-2022, 08:29 AM
You can write the code as follows to display values if TypeError encountered:
import numpy as np import sys A = np.array([1, 2, 3]) M = np.zeros((10, 10), dtype=float) N = np.copy(M) P = np.copy(M) # Explicitly on 1 location r = 3 c = 2 M[r, c : c + 3] = A print(f"\nM(r, c:c_3): {M[r, c:c+3]}") # now on 2 locations # with a loop r = np.array([3, 5]) c = np.array([2, 5]) for i in range(2): try: N[r[i], c[i] : c[i] + 3] = A except TypeError: print(f"First loop: Values when TypeError occurs:, i: {i}, c[i]: {c[i]}") sys.exit(-1) # without a loop i = np.arange(2) j = c[i] + 3 * np.ones(2, dtype=int) k = np.ones((2, 1), dtype=int) * A try: P[r[i], c[i] : j[i]] = k[i] except TypeError: print( f"\nTypeError, Values when attempting P[r[i], c[i]:j[i]]=k[i]:\n" f" i: {i}, r[i]: {r[i]}, c[i]: {c[i]}, j[i]: {j[i]}" ) sys.exit(-1)output shows where error occurs:
Output:M(r, c:c_3): [1. 2. 3.]
TypeError, Values when attempting P[r[i], c[i]:j[i]]=k[i]:
i: [0 1], r[i]: [3 5], c[i]: [2 5], j[i]: [5 8]