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:
Double sum question

Subject: Double sum question

From: Marios Karaoulis

Date: 17 Jun, 2010 02:05:35

Message: 1 of 9

I can't find a quick way to execute the following double sum.

i=integer
j=integer
b_node 3x1 vector
c_node 3x1 vector


% sum
x_flux1=((a(i,a1)*b_node(1)+a(i,a2)*b_node(2)+a(i,a3)*b_node(3)));
x_flux2=((a(j,a1)*b_node(1)+a(j,a2)*b_node(2)+a(j,a3)*b_node(3)));

y_flux1=((a(i,a1)*c_node(1)+a(i,a2)*c_node(2)+a(i,a3)*c_node(3)));
y_flux2=((a(j,a1)*c_node(1)+a(j,a2)*c_node(2)+a(j,a3)*c_node(3)));


jam2=x_flux1*x_flux2+y_flux1*y_flux2;


i tried this
tmp_a(1)=a(i,a1);
tmp_a(2)=a(i,a2);
tmp_a(3)=a(i,a3);

tmp_b(1)=a(i,a1);
tmp_b(3)=a(i,a2);
tmp_b(3)=a(i,a3);

stable=((b_node*b_node'+c_node*c_node');
a_11=sum ( sum( stable .* (tmp_a*tmp_b' ) ) );

but the results are not the same.
Where is the error?

Subject: Double sum question

From: Roger Stafford

Date: 17 Jun, 2010 03:05:09

Message: 2 of 9

Marios Karaoulis <marios.karaoulis@gmail.com> wrote in message <34f86e08-19ab-49a9-a3c0-e908bd93de65@j12g2000pri.googlegroups.com>...
> I can't find a quick way to execute the following double sum.
>
> i=integer
> j=integer
> b_node 3x1 vector
> c_node 3x1 vector
>
>
> % sum
> x_flux1=((a(i,a1)*b_node(1)+a(i,a2)*b_node(2)+a(i,a3)*b_node(3)));
> x_flux2=((a(j,a1)*b_node(1)+a(j,a2)*b_node(2)+a(j,a3)*b_node(3)));
>
> y_flux1=((a(i,a1)*c_node(1)+a(i,a2)*c_node(2)+a(i,a3)*c_node(3)));
> y_flux2=((a(j,a1)*c_node(1)+a(j,a2)*c_node(2)+a(j,a3)*c_node(3)));
>
>
> jam2=x_flux1*x_flux2+y_flux1*y_flux2;
>
>
> i tried this
> tmp_a(1)=a(i,a1);
> tmp_a(2)=a(i,a2);
> tmp_a(3)=a(i,a3);
>
> tmp_b(1)=a(i,a1);
> tmp_b(3)=a(i,a2);
> tmp_b(3)=a(i,a3);
>
> stable=((b_node*b_node'+c_node*c_node');
> a_11=sum ( sum( stable .* (tmp_a*tmp_b' ) ) );
>
> but the results are not the same.
> Where is the error?

  If tmp_a and tmp_b are row vectors, the product tmp_a*tmp_b' will be a scalar which is definitely not what you want! Do an initial allocation of them as column vectors before entering data into them and see if that doesn't improve things.

Roger Stafford

Subject: Double sum question

From: Marios Karaoulis

Date: 17 Jun, 2010 14:59:43

Message: 3 of 9


>
>   If tmp_a and tmp_b are row vectors, the product tmp_a*tmp_b' will be a scalar which is definitely not what you want!  Do an initial allocation of them as column vectors before entering data into them and see if that doesn't improve things.
>


tmp_a is a 3x1
tmp_b is a 3x1

so, tmp_a*tmp_b' is a 3x3 matrix

Subject: Double sum question

From: Matt J

Date: 17 Jun, 2010 16:12:20

Message: 4 of 9

Marios Karaoulis <marios.karaoulis@gmail.com> wrote in message <34f86e08-19ab-49a9-a3c0-e908bd93de65@j12g2000pri.googlegroups.com>...
> I can't find a quick way to execute the following double sum.
>
> i=integer
> j=integer
> b_node 3x1 vector
> c_node 3x1 vector
>
>
> % sum
> x_flux1=((a(i,a1)*b_node(1)+a(i,a2)*b_node(2)+a(i,a3)*b_node(3)));
> x_flux2=((a(j,a1)*b_node(1)+a(j,a2)*b_node(2)+a(j,a3)*b_node(3)));
>
> y_flux1=((a(i,a1)*c_node(1)+a(i,a2)*c_node(2)+a(i,a3)*c_node(3)));
> y_flux2=((a(j,a1)*c_node(1)+a(j,a2)*c_node(2)+a(j,a3)*c_node(3)));
>
>
> jam2=x_flux1*x_flux2+y_flux1*y_flux2;
===============

Looks like you want the following

A=[a(i,a1), a(i,a2), a(i,a3);
  a(j,a1), a(j,a2), a(j,a3)];

jam2=sum( prod( A*[b_node,c_node], 1 ) );

Subject: Double sum question

From: Marios Karaoulis

Date: 17 Jun, 2010 16:41:49

Message: 5 of 9

On Jun 17, 10:12 am, "Matt J " <mattjacREM...@THISieee.spam> wrote:
> Marios Karaoulis <marios.karaou...@gmail.com> wrote in message <34f86e08-19ab-49a9-a3c0-e908bd93d...@j12g2000pri.googlegroups.com>...
> > I can't find a quick way to execute the following double sum.
>
> > i=integer
> > j=integer
> > b_node 3x1 vector
> > c_node 3x1 vector
>
> > %  sum
> > x_flux1=((a(i,a1)*b_node(1)+a(i,a2)*b_node(2)+a(i,a3)*b_node(3)));
> > x_flux2=((a(j,a1)*b_node(1)+a(j,a2)*b_node(2)+a(j,a3)*b_node(3)));
>
> > y_flux1=((a(i,a1)*c_node(1)+a(i,a2)*c_node(2)+a(i,a3)*c_node(3)));
> > y_flux2=((a(j,a1)*c_node(1)+a(j,a2)*c_node(2)+a(j,a3)*c_node(3)));
>
> > jam2=x_flux1*x_flux2+y_flux1*y_flux2;
>
> ===============
>
> Looks like you want the following
>
> A=[a(i,a1),  a(i,a2), a(i,a3);
>   a(j,a1),  a(j,a2), a(j,a3)];
>
> jam2=sum(  prod( A*[b_node,c_node], 1 )  );

All right, that't it
Thanks

Subject: Double sum question

From: Marios Karaoulis

Date: 17 Jun, 2010 17:01:53

Message: 6 of 9

I have another double sum, (if you don't mind)

s1=a(i,a1)+a(i,a2)+a(i,a3);
s2=a(j,a1)+a(j,a2)+a(j,a3);
sum1=2*(a(i,a1)*a(j,a1)+a(i,a2)*a(j,a2)+a(i,a3)*a(j,a3))+s1*s2;

I tried creating this matrix
um=[2 1 1;
    1 2 1;
    1 1 2];


and then

jam4=sum(prod(A*um,1));


P.S., it would be great if I could combine the previous two sums, in
just one equation!!!

Thanks

Subject: Double sum question

From: Matt J

Date: 17 Jun, 2010 17:34:22

Message: 7 of 9

Marios Karaoulis <marios.karaoulis@gmail.com> wrote in message <b14ebba5-2866-4fd8-b341-f83178c5091b@n37g2000prc.googlegroups.com>...
> I have another double sum, (if you don't mind)
>
> s1=a(i,a1)+a(i,a2)+a(i,a3);
> s2=a(j,a1)+a(j,a2)+a(j,a3);
> sum1=2*(a(i,a1)*a(j,a1)+a(i,a2)*a(j,a2)+a(i,a3)*a(j,a3))+s1*s2;
>
> I tried creating this matrix
> um=[2 1 1;
> 1 2 1;
> 1 1 2];
>
>
> and then
>
> jam4=sum(prod(A*um,1));
=================

It's inadvisable to try random things without knowing if/why they might work.




 
> P.S., it would be great if I could combine the previous two sums, in
> just one equation!!!
===========

sum1=A(1,:)*um*A(2,:).'

Subject: Double sum question

From: Marios Karaoulis

Date: 17 Jun, 2010 17:57:27

Message: 8 of 9

On Jun 17, 11:34 am, "Matt J " <mattjacREM...@THISieee.spam> wrote:
> Marios Karaoulis <marios.karaou...@gmail.com> wrote in message <b14ebba5-2866-4fd8-b341-f83178c50...@n37g2000prc.googlegroups.com>...
> > I have another double sum, (if you don't mind)
>
> > s1=a(i,a1)+a(i,a2)+a(i,a3);
> > s2=a(j,a1)+a(j,a2)+a(j,a3);
> > sum1=2*(a(i,a1)*a(j,a1)+a(i,a2)*a(j,a2)+a(i,a3)*a(j,a3))+s1*s2;
>
> > I tried creating this matrix
> > um=[2 1 1;
> >     1 2 1;
> >     1 1 2];
>
> > and then
>
> > jam4=sum(prod(A*um,1));
>
> =================
>
> It's inadvisable to try random things without knowing if/why they might work.
>
> > P.S., it would be great if I could combine the previous two sums, in
> > just one equation!!!
>
> ===========
>
> sum1=A(1,:)*um*A(2,:).'

Thanks again, and I shouldn't try random thinks).

What i want to do is to minimize calculation because I need these sums
aground 1.000.0000 times, which makes everything really slow.

So, as a final request, how can i make the following in just one step
(use the second sum in the product somehow...)
currently I use this


final= sum( prod( A*[b_node,c_node], 1 ) ) + A(1,:)*um*A(2,:).';

Thanks for all your help.

Subject: Double sum question

From: Steven Lord

Date: 17 Jun, 2010 21:15:26

Message: 9 of 9


"Marios Karaoulis" <marios.karaoulis@gmail.com> wrote in message
news:3d79ab13-9066-417d-af51-4d636d2ed2c8@s4g2000prh.googlegroups.com...

*snip*

> Thanks again, and I shouldn't try random thinks).
>
> What i want to do is to minimize calculation because I need these sums
> aground 1.000.0000 times, which makes everything really slow.

Start at the beginning. Why do you need to perform these computations a
million (I assume the last 0 is a typo) times? If you explain a little more
about the problem you're trying to solve, someone may be able to suggest a
more efficient approach.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

No tags are associated with 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