Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
speed up my cross product of elements in one vector

Subject: speed up my cross product of elements in one vector

From: Skirt Zhang

Date: 25 May, 2011 09:13:04

Message: 1 of 6

Hi,

I have a vector b=[b1 b2 ....bN]'

and I want to get their un-replicated cross products like

b1*b2*b3, b1*b2*b4, b1*b2*b5 ...

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.

 
aa=[];
for i=1:length(a)
  for j=1:length(a)
      for k=1:length(a)
      
          if (j~=i)&&(k~=i)&&(k~=j)
    ae=a(i)*a(j)*a(k);
          else
              ae=0;
          end
       aa=[aa ae];
      end
      
  
  end

end

Subject: speed up my cross product of elements in one vector

From: Rune Allnor

Date: 25 May, 2011 09:28:32

Message: 2 of 6

On May 25, 11:13 am, "Skirt Zhang" <silence_qu...@hotmail.com> wrote:
> Hi,
>
> I have a vector b=[b1 b2 ....bN]'
>
> and I want to get their un-replicated cross products like
>
> b1*b2*b3,  b1*b2*b4, b1*b2*b5  ...
>
> 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.
...
>        aa=[aa ae];  

One thing you can do, that will have an significant impact,
is to figure out in advance how many terms you will get,
and pre-allocate the array aa.

Rune  

Subject: speed up my cross product of elements in one vector

From: Skirt Zhang

Date: 25 May, 2011 09:33:05

Message: 3 of 6

Dear Rune,

Thanks for your quick reply. Can I understand the "pre-allocate" as:

expect that I will get a vector of size N^4
 so I define aa=zeros(N^4,1), and in each loop aa(i)=...?

best regards

 


Rune Allnor <allnor@tele.ntnu.no> wrote in message <8634c5a4-99e4-4730-8175-721b1934c30e@q32g2000yqn.googlegroups.com>...
> On May 25, 11:13 am, "Skirt Zhang" <silence_qu...@hotmail.com> wrote:
> > Hi,
> >
> > I have a vector b=[b1 b2 ....bN]'
> >
> > and I want to get their un-replicated cross products like
> >
> > b1*b2*b3,  b1*b2*b4, b1*b2*b5  ...
> >
> > 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.
> ...
> >        aa=[aa ae];  
>
> One thing you can do, that will have an significant impact,
> is to figure out in advance how many terms you will get,
> and pre-allocate the array aa.
>
> Rune  

Subject: speed up my cross product of elements in one vector

From: Skirt Zhang

Date: 25 May, 2011 09:43:04

Message: 4 of 6

I have 180^3 for the length of aa....

Is it feasible ?

Rune Allnor <allnor@tele.ntnu.no> wrote in message <8634c5a4-99e4-4730-8175-721b1934c30e@q32g2000yqn.googlegroups.com>...
> On May 25, 11:13 am, "Skirt Zhang" <silence_qu...@hotmail.com> wrote:
> > Hi,
> >
> > I have a vector b=[b1 b2 ....bN]'
> >
> > and I want to get their un-replicated cross products like
> >
> > b1*b2*b3,  b1*b2*b4, b1*b2*b5  ...
> >
> > 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.
> ...
> >        aa=[aa ae];  
>
> One thing you can do, that will have an significant impact,
> is to figure out in advance how many terms you will get,
> and pre-allocate the array aa.
>
> Rune  

Subject: speed up my cross product of elements in one vector

From: Roger Stafford

Date: 25 May, 2011 20:47:05

Message: 5 of 6

"Skirt Zhang" <silence_qunzi@hotmail.com> wrote in message <irih70$3cu$1@newscl01ah.mathworks.com>...
> Hi,
> I have a vector b=[b1 b2 ....bN]'
> and I want to get their un-replicated cross products like
> b1*b2*b3, b1*b2*b4, b1*b2*b5 ...
> ........
- - - - - - - - -
  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.

 n = size(b,1);
 aa = zeros(1,n*(n-1)*(n-2)/6);
 c1 = 0;
 for k = 2:n-1
   c2 = c1+(k-1)*(n-k);
   aa(c1+1:c2) = b(1:k-1)*b(k)*b(k+1:n).';
   c1 = c2;
 end

Roger Stafford

Subject: speed up my cross product of elements in one vector

From: Skirt Zhang

Date: 26 May, 2011 12:28:04

Message: 6 of 6

Dear Roger,

It really works well!!

Thanks a lot!:))

"Roger Stafford" wrote in message <irjps9$43a$1@newscl01ah.mathworks.com>...
> "Skirt Zhang" <silence_qunzi@hotmail.com> wrote in message <irih70$3cu$1@newscl01ah.mathworks.com>...
> > Hi,
> > I have a vector b=[b1 b2 ....bN]'
> > and I want to get their un-replicated cross products like
> > b1*b2*b3, b1*b2*b4, b1*b2*b5 ...
> > ........
> - - - - - - - - -
> 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.
>
> n = size(b,1);
> aa = zeros(1,n*(n-1)*(n-2)/6);
> c1 = 0;
> for k = 2:n-1
> c2 = c1+(k-1)*(n-k);
> aa(c1+1:c2) = b(1:k-1)*b(k)*b(k+1:n).';
> c1 = c2;
> end
>
> Roger Stafford

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us