2

Is there any way to replace elements of a matrix with other elements from a dictionary in constant time without using loops. For example I have this initial matrix:

[[3 1 2]
 [3 1 2]
 [3 1 2]]

Then I have computed this dictionary {1: 5, 2: 6, 3: 4, 8: 9, 5: 1, 6: 2, 4: 3, 9: 8} by some operations. What I want to do is change the initial matrix to:

[[4 5 6]
 [4 5 6]
 [4 5 6]]

As you see, the keys in the dictionary are the numbers in first matrix and values are the numbers in second matrix. I can do that by looping every cell but that would be slow. Is there any fast way to do that?

warped
  • 8,947
  • 3
  • 22
  • 49
gunesevitan
  • 882
  • 10
  • 25
  • 1
    Does this answer your question? [Efficiently replace elements in array based on dictionary - NumPy / Python](https://stackoverflow.com/questions/55949809/efficiently-replace-elements-in-array-based-on-dictionary-numpy-python) – warped Feb 23 '20 at 09:08
  • @warped I guess there is no direct way of doing that. All answers are some kind of workarounds. If that's the case, it does answer my question. – gunesevitan Feb 23 '20 at 09:35

0 Answers0