-1

I got the following:

  • redIdx: a 2x1 matrix with values (289485, 289486).
  • image: 366x791x3 uint8 matrix (an image).
  • image2: zeros matrix with the same sape as image.

In MATLAB, if I do image2(redIdx) it returns a 2x1 matrix with values (0,0) and if I do image(redIdx) it returns a 2x1 matrix with values (94, 83).

But in Python, if I do image2[redIdx] or image[redIdx], it returns the next error: index 2879485 is out of bounds for axis 0 with size 366.

How can I get the same result as MATLAB?

2
  • 1
    It seems like you want linear indexing in Python. See if this helps Commented Sep 7, 2021 at 10:46
  • I'm trying but don't know yet how to do it Commented Sep 7, 2021 at 11:01

1 Answer 1

1

MATLAB, when indexing an array with a single index (as opposed to multiple ones) uses linear indexing. Python, in the same situation, uses the index to index into the first dimension, returning a slice. The fact that redIdx contains multiple values is irrelevant, it's a 1D indexing operation.

To replicate linear indexing in Python, you can flatten the array, then index:

image.flatten('K')[redIdx]

This Q&A shows how to compute indices from the single linear index, which would be a more complex alternative to the above.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.