help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Applying each element of a vector to each row of a matrix


From: Martin Helm
Subject: Re: Applying each element of a vector to each row of a matrix
Date: Mon, 20 Dec 2010 02:46:44 +0100
User-agent: KMail/1.13.5 (Linux/2.6.34.7-0.5-desktop; KDE/4.5.4; x86_64; ; )

Am Montag, 20. Dezember 2010, 02:06:36 schrieb Judd Storrs:
> The bsxfun function does a lot of this. Assume
> 
> a = ones(10) ;
> v = 1:10 ;
> 
> Multiply each column of a by the corresponding element of v:
> 
> bsxfun(@times, a, v)
> 
> Multiply each row of a by the corresponding element of v:
> 
> bsxfun(@times, a, v')
> 
> Raise each column of a to the corresponding power of v:
> 
> bsxfun(@power, a, v)
> 
> Raise each row of a to the corresponding power of v:
> 
> bsxfun(@power, a, v')
> 
> 
> --judd

Thanks for that, I did not even think about the bsxfun. For the simple 
multiplication I still prefer the diag(v)*a way since it is more performant

octave:1> v = rand(1000,1);
octave:2> a = rand(1000);
octave:3> tic;diag(v)*a;toc
Elapsed time is 0.00667596 seconds.
octave:4> tic;bsxfun(@times, a, v);toc
Elapsed time is 0.0379739 seconds.

For everything else it is a perfect solution.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]