sihariru sueiaran
sihariru sueiaran

Reputation: 140

Convert function from matlab to python

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

Upvotes: 0

Views: 163

Answers (1)

Nils Werner
Nils Werner

Reputation: 36849

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)

Upvotes: 2

Related Questions