python - MATLAB ind2sub and Numpy unravel_index inconsistency -
based on following answer:
using octave, get:
>> [x, y, z] = ind2sub([27, 5, 58], 3766) x = 13 y = 5 z = 28
using numpy, get:
>>> import numpy np >>> np.unravel_index(3765, (27, 5, 58)) (12, 4, 53)
why, in numpy, z
component 58, when should 27 according octave?
well matlab follows column-major indexing, (x,y,z)
elements stored @ x
, y
, z
. numpy (x,y,z)
because of row-major indexing, it's other way - z
, y
, x
. so, replicate same behavior in numpy, need flip grid shape use np.unravel_index
, flip output indices, -
np.unravel_index(3765, (58, 5, 27))[::-1]
sample run -
in [18]: np.unravel_index(3765, (58, 5, 27))[::-1] out[18]: (12, 4, 27)
Comments
Post a Comment