-2

Having an array (2d) with index of colors is there a way to replace the index by its color in one step not element by element?

Example:

colors=[[255,0,0],[125,222,11]]
im_x=[[0,0],[1,0]]

#the result must be:

im_c= [[[255,0,0],[255,0,0]],[[125,222,11],[255,0,0]]

I have try colors[im_x] but it does not work :(

2
  • 1
    The problem is that you have not defined arrays... Commented May 1, 2019 at 16:34
  • Can you please be a bit more specific? Commented May 1, 2019 at 16:35

1 Answer 1

1

Define the arrays as numpy arrays, and then index them as you were trying to:

import numpy as np
colors = np.array([[255,0,0],[125,222,11]])
im_x = np.array([[0,0],[1,0]])

colors[im_x]

array([[[255,   0,   0],
        [255,   0,   0]],

       [[125, 222,  11],
        [255,   0,   0]]])
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.