[Numpy-discussion] multiply array

Fabrice Silva silva@lma.cnrs-mrs...
Fri Apr 4 05:29:29 CDT 2008



Le vendredi 04 avril 2008 à 00:28 -0700, wilson a écrit :
> > #of shape (1,6)
> > eval=array([[3.,3.2,1.,1.1,5.,0.5]])
> >
> 
> eval.shape=(-1,)
> 
> please not the correction..i need to multiply first row of egimgs with
> 3.0 ,second row with 3.2,....last(sixth) row with 0.5 ..For that
> purpose i made the above into a 1 dimensional array.
> A for loop seems inefficient in the case of big arrays
> W

Hi
I suggest you three ways:
* using matrices :
        mat(eval*identity(eval.shape[1]))*mat(egimgs)
        
        creates first a diagonal matrix with eval coefficients on main
        diagonal, then multiply the matrices diagonal and egimgs
* using a temporary matrix with repeated eval coefficients
        eval.T.repeat(egimgs.shape[1],axis=1)*egimgs
        
        repeats array eval as many times as required to have two arrays
        of the same size that can be multiplied.

and maybe the more efficient that does not require you to create a temp
array of size egimgs.shape :
        eval.T*egimgs
	or for inplace update of egimgs
	egimgs *= eval.T
        
        provided the len of 1darray and one of the dimension of the
        2darray match, you can multiply them directly.


-- 
Fabrice Silva, PhD student
LMA UPR CNRS 7051 - équipe S2M



More information about the Numpy-discussion mailing list