1
votes

I'm trying to convert my MATLAB code to Python.

In MATLAB I have OP which is a 300x300 double array and mask which is 300x300 logical array.

t1 = mask(:) equals 90000x1 logical array.

How is it possible that the output of t2 = OP(mask(:)) equals to a 57664x1 double array?

Here my MATLAB code :

OP=repmat(Ph,size(image,1),1).*repmat(Pv,1,size(image,2));
t1 = mask(:)
t2 = OP(mask(:))
data=sort(OP(mask(:)),'descend'); 

Also, in Python I use Numpy to implement my MATLAB code but OP[mask] which is MATLAB converted OP(mask(:)) is a 90000x1x300 ndarray. I don't know how to fix it.

Here my python code:

OP = np.matlib.repmat(Ph, image.shape[0], 1) * np.matlib.repmat(Pv, 1, image.shape[1])
t2 = OP[mask]
data = -np.sort(-OP[mask], axis=0)

I know that t1, OP and mask have the same size as its similar variable in MATLAB.

I would be appreciative if anybody could help me.

1
Can you rephrase your question as a MCVE? - Paolo

1 Answers

0
votes

In an Octave session:

>> OP = reshape(1:16,4,4);
>> OP
OP =

    1    5    9   13
    2    6   10   14
    3    7   11   15
    4    8   12   16

>> mask = logical([1,0,0,1;0,1,1,1;0,0,0,0;1,0,1,0])
mask =

  1  0  0  1
  0  1  1  1
  0  0  0  0
  1  0  1  0

>> OP(mask)
ans =

    1
    4
    6
   10
   12
   13
   14

ravel doesn't make a difference:

>> OP(mask(:))
ans =

    1
    4
    6
   10
   12
   13
   14

In a ipython/numpy session:

In [368]: OP = np.arange(1,17).reshape(4,4)                                                            
In [369]: OP                                                                                           
Out[369]: 
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12],
       [13, 14, 15, 16]])
In [370]: mask = np.array([[1,0,0,1],[0,1,1,1],[0,0,0,0],[1,0,1,0]]).astype(bool)                      
In [371]: mask                                                                                         
Out[371]: 
array([[ True, False, False,  True],
       [False,  True,  True,  True],
       [False, False, False, False],
       [ True, False,  True, False]])
In [372]: OP[mask]                                                                                     
Out[372]: array([ 1,  4,  6,  7,  8, 13, 15])