http://www.mathworks.com/matlabcentral/newsreader/view_thread/308281
MATLAB Central Newsreader  speed up my cross product of elements in one vector
Feed for thread: speed up my cross product of elements in one vector
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Wed, 25 May 2011 09:13:04 +0000
speed up my cross product of elements in one vector
http://www.mathworks.com/matlabcentral/newsreader/view_thread/308281#837793
Skirt Zhang
Hi,<br>
<br>
I have a vector b=[b1 b2 ....bN]'<br>
<br>
and I want to get their unreplicated cross products like<br>
<br>
b1*b2*b3, b1*b2*b4, b1*b2*b5 ...<br>
<br>
I came up with this code as below , however when I have N=200 the computation time is tremendous..:( Can anyone help me to speed up this calculation? Thanks a lot in advance.<br>
<br>
<br>
aa=[]; <br>
for i=1:length(a)<br>
for j=1:length(a)<br>
for k=1:length(a)<br>
<br>
if (j~=i)&&(k~=i)&&(k~=j)<br>
ae=a(i)*a(j)*a(k);<br>
else<br>
ae=0;<br>
end<br>
aa=[aa ae]; <br>
end<br>
<br>
<br>
end<br>
<br>
end

Wed, 25 May 2011 09:28:32 +0000
Re: speed up my cross product of elements in one vector
http://www.mathworks.com/matlabcentral/newsreader/view_thread/308281#837795
Rune Allnor
On May 25, 11:13 am, "Skirt Zhang" <silence_qu...@hotmail.com> wrote:<br>
> Hi,<br>
><br>
> I have a vector b=[b1 b2 ....bN]'<br>
><br>
> and I want to get their unreplicated cross products like<br>
><br>
> b1*b2*b3, b1*b2*b4, b1*b2*b5 ...<br>
><br>
> I came up with this code as below , however when I have N=200 the computation time is tremendous..:( Can anyone help me to speed up this calculation? Thanks a lot in advance.<br>
...<br>
> aa=[aa ae]; <br>
<br>
One thing you can do, that will have an significant impact,<br>
is to figure out in advance how many terms you will get,<br>
and preallocate the array aa.<br>
<br>
Rune

Wed, 25 May 2011 09:33:05 +0000
Re: speed up my cross product of elements in one vector
http://www.mathworks.com/matlabcentral/newsreader/view_thread/308281#837797
Skirt Zhang
Dear Rune,<br>
<br>
Thanks for your quick reply. Can I understand the "preallocate" as:<br>
<br>
expect that I will get a vector of size N^4 <br>
so I define aa=zeros(N^4,1), and in each loop aa(i)=...?<br>
<br>
best regards<br>
<br>
<br>
<br>
<br>
Rune Allnor <allnor@tele.ntnu.no> wrote in message <8634c5a499e447308175721b1934c30e@q32g2000yqn.googlegroups.com>...<br>
> On May 25, 11:13 am, "Skirt Zhang" <silence_qu...@hotmail.com> wrote:<br>
> > Hi,<br>
> ><br>
> > I have a vector b=[b1 b2 ....bN]'<br>
> ><br>
> > and I want to get their unreplicated cross products like<br>
> ><br>
> > b1*b2*b3, b1*b2*b4, b1*b2*b5 ...<br>
> ><br>
> > I came up with this code as below , however when I have N=200 the computation time is tremendous..:( Can anyone help me to speed up this calculation? Thanks a lot in advance.<br>
> ...<br>
> > aa=[aa ae]; <br>
> <br>
> One thing you can do, that will have an significant impact,<br>
> is to figure out in advance how many terms you will get,<br>
> and preallocate the array aa.<br>
> <br>
> Rune

Wed, 25 May 2011 09:43:04 +0000
Re: speed up my cross product of elements in one vector
http://www.mathworks.com/matlabcentral/newsreader/view_thread/308281#837798
Skirt Zhang
I have 180^3 for the length of aa.... <br>
<br>
Is it feasible ?<br>
<br>
Rune Allnor <allnor@tele.ntnu.no> wrote in message <8634c5a499e447308175721b1934c30e@q32g2000yqn.googlegroups.com>...<br>
> On May 25, 11:13 am, "Skirt Zhang" <silence_qu...@hotmail.com> wrote:<br>
> > Hi,<br>
> ><br>
> > I have a vector b=[b1 b2 ....bN]'<br>
> ><br>
> > and I want to get their unreplicated cross products like<br>
> ><br>
> > b1*b2*b3, b1*b2*b4, b1*b2*b5 ...<br>
> ><br>
> > I came up with this code as below , however when I have N=200 the computation time is tremendous..:( Can anyone help me to speed up this calculation? Thanks a lot in advance.<br>
> ...<br>
> > aa=[aa ae]; <br>
> <br>
> One thing you can do, that will have an significant impact,<br>
> is to figure out in advance how many terms you will get,<br>
> and preallocate the array aa.<br>
> <br>
> Rune

Wed, 25 May 2011 20:47:05 +0000
Re: speed up my cross product of elements in one vector
http://www.mathworks.com/matlabcentral/newsreader/view_thread/308281#837915
Roger Stafford
"Skirt Zhang" <silence_qunzi@hotmail.com> wrote in message <irih70$3cu$1@newscl01ah.mathworks.com>...<br>
> Hi,<br>
> I have a vector b=[b1 b2 ....bN]'<br>
> and I want to get their unreplicated cross products like<br>
> b1*b2*b3, b1*b2*b4, b1*b2*b5 ...<br>
> ........<br>
        <br>
As your code is at present you have many occurrences of zeros where a factor would have been repeated. Also each product of three different factors will occur in six permutations which is costing you cpu time. The following will compute a product of every possible subset of three factors just once each, and there will be no zeros (unless 'b' has them.) I assume you don't care about the order of products in 'aa'. This code assumes that 'b' is a column vector, as you have indicated.<br>
<br>
n = size(b,1);<br>
aa = zeros(1,n*(n1)*(n2)/6);<br>
c1 = 0;<br>
for k = 2:n1<br>
c2 = c1+(k1)*(nk);<br>
aa(c1+1:c2) = b(1:k1)*b(k)*b(k+1:n).';<br>
c1 = c2;<br>
end<br>
<br>
Roger Stafford

Thu, 26 May 2011 12:28:04 +0000
Re: speed up my cross product of elements in one vector
http://www.mathworks.com/matlabcentral/newsreader/view_thread/308281#838040
Skirt Zhang
Dear Roger,<br>
<br>
It really works well!!<br>
<br>
Thanks a lot!:))<br>
<br>
"Roger Stafford" wrote in message <irjps9$43a$1@newscl01ah.mathworks.com>...<br>
> "Skirt Zhang" <silence_qunzi@hotmail.com> wrote in message <irih70$3cu$1@newscl01ah.mathworks.com>...<br>
> > Hi,<br>
> > I have a vector b=[b1 b2 ....bN]'<br>
> > and I want to get their unreplicated cross products like<br>
> > b1*b2*b3, b1*b2*b4, b1*b2*b5 ...<br>
> > ........<br>
>         <br>
> As your code is at present you have many occurrences of zeros where a factor would have been repeated. Also each product of three different factors will occur in six permutations which is costing you cpu time. The following will compute a product of every possible subset of three factors just once each, and there will be no zeros (unless 'b' has them.) I assume you don't care about the order of products in 'aa'. This code assumes that 'b' is a column vector, as you have indicated.<br>
> <br>
> n = size(b,1);<br>
> aa = zeros(1,n*(n1)*(n2)/6);<br>
> c1 = 0;<br>
> for k = 2:n1<br>
> c2 = c1+(k1)*(nk);<br>
> aa(c1+1:c2) = b(1:k1)*b(k)*b(k+1:n).';<br>
> c1 = c2;<br>
> end<br>
> <br>
> Roger Stafford