0

I have three 2D arrays each, each of shape [180 x 360]

I want make a new array which contains only regions with the maximum of each array compared to the other two.

 # Assuming these are my 2D arrays A = np.array([[2, 6], [4, 2]]) B = np.array([[1, 4], [5, 2]]) C = np.array([[5, 4], [8, 4]]) # Trying to get the maximum regions when compared to the two arrays AA = np.logical_and(A>B, A>C) BB = np.logical_and(B>A, B>C) CC = np.logical_and(C>A, C>B) # in this case AA shoud be new array like this AA = [False,True][False,False] 

Problem here is that the reuslts I get overlap. I expect AA, BB, CC to be unique given the fact that each of them represent maximum of the other two. How can I make this right?

Thanks

3
  • Not sure if what you mean, but maybe np.maximum(A, np.maximum(B, C))? Commented Aug 8, 2019 at 9:41
  • make a verifiable example. Provide toy inputs and expected output and you're guaranteed to get help. If it works on 3 2x2 arrays it can be made to work on 3 180x360 arrays Commented Aug 8, 2019 at 9:44
  • @ kevinkayaks edited accordingly. Hope its clearer now? Commented Aug 8, 2019 at 10:05

1 Answer 1

1

You can just stack your matrices and take the argmax:

import numpy as np idx = np.argmax(np.stack((A, B, C)), axis=0) 

The returned index is your answer. Compare it to 0, 1, 2 to get the boolean masks:

 masks = idx[None, ...] == np.arange(3)[:, None, None] 

Edit: You can also plot the output as an RGB image encoding the maximum value as a red, green and blue pixel by

  1. Transposing the mask, so that the A/B/C masks are in the third dimension,
  2. casting the result to integers
  3. multiplying the result with 255 (0 is black, 255 is maximum brightness in each channel)

which can be done using

 import matplotlib.pyplot as plt plt.imshow(masks.transpose(1, 2, 0).astype(int) * 255) 
Sign up to request clarification or add additional context in comments.

1 Comment

@ Nils Werner Can this help me get regions of where only A dominates for examples and color such regions blue, get regions where b dominates and color it green, get regions where green dominates and color it red?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.