python - Need help converting Matlab's bsxfun to numpy -
i'm trying convert piece of matlab code, , line i'm struggling with:
f = 0 wlab = reshape(bsxfun(@times,cat(3,1-f,f/2,f/2),lab),[],3)
i've come
wlab = lab*(np.concatenate((3,1-f,f/2,f/2)))
how reshape now?
matlab
reshape(x,[],3)
is equivalent of numpy
np.reshape(x,(-1,3))
the []
, -1
place holders 'fill in correct shape here'.
===============
i tried matlab expression octave - it's on different machine, i'll summarize action.
for lab=1:6
(6 elements) bsxfun
produces (1,6,3) matrix; reshape
turns (6,3), i.e. removes first dimension. cat
produces (1,1,3) matrix.
np.reshape(np.array([1-f,f/2,f/2])[none,none,:]*lab[none,:,none],(-1,3))
for lab
shape (n,m), bsxfun
produces (n,m,3) matrix; reshape make (n*m,3)
so 2d lab
, numpy
needs be
np.array([1-f,f/2,f/2])[none,none,:]*lab[:,:,none]
(in matlab lab
2d (or larger), 2nd case closer action if n
1).
=======================
np.array([1-f,f/2,f/2])*lab[...,none]
would handle shaped lab
if make octave lab
(4,2,3), `bsxfun (4,2,3)
the matching numpy expression be
in [94]: (np.array([1-f,f/2,f/2])*lab).shape out[94]: (4, 2, 3)
numpy
adds dimensions start of (3,) array match dimensions of lab
, effectively
(np.array([1-f,f/2,f/2])[none,none,:]*lab) # 3d lab
if f=0
, array [1,0,0]
, has effect of zeroing values on last dimension of lab
. in effect, changing 'color'.
Comments
Post a Comment