0

I am new to the python. I have a problem to convert this simple function in matlab to the python. Here is the original matlab code

function Cmode=my_map(image1)

[D,A,B] = size(image1)
crp = image1((1:D),(A:-1:1),(B:-1:1));
Cmode1 = max(crp,[],1);
Cmode = permute(Cmode1,[3 2 1]);

The input file is 3d matrix. I tried this but failed

def cmode(image1):
   [D,A,B] = np.shape(image1)
   crp = image1[(0,D),(A,-1,0), (B,-1,0)]
   cmode1 = np.max(crp,[],1)
   c = np.transpose( np.expand_dims(cmode1, axis=2), (2, 1, 0) )
   return c

here is the error

crp = d3image[(0,D),(A,-1,0), (B,-1,0)]
IndexError: shape mismatch: indexing arrays could not be broadcast together      with shapes (2,) (3,) (3,) 

Any help will be appreciated. Thank you

1
  • You should change the title of this question to better reflect what the function does. That way it may help more SO users in future. Commented Mar 13, 2017 at 11:20

1 Answer 1

2

D, A and B are unnecessary, they are only being used to read the image from end to end (second and third axes read backwards). In NumPy that notation would be

crp = image1[:, ::-1, ::-1]

Secondly, the second argument for max won't work, instead you should use

cmode1 = np.max(crp, axis=1)
Sign up to request clarification or add additional context in comments.

1 Comment

np.max(image1,axis=0, keepdims=1) because MATLAB keeps the dims, to be strictly coherent with MATLAB's version. And then you would need something like : np.max(image1,axis=0, keepdims=1)[0].T to remove that extra dim. If you are in hurry to get to the final destination : np.max(image1,axis=0).T.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.