From: keisyu keisyu on
How following summation should be calculated by MATLAB withtout
For loop ?

Σ(a:0->999)Σ(b:0->999) f(a,b)

where, f(a,b)=2a+b;
From: Walter Roberson on
keisyu keisyu wrote:
> How following summation should be calculated by MATLAB withtout
> For loop ?
>
> Σ(a:0->999)Σ(b:0->999) f(a,b)
>
> where, f(a,b)=2a+b;

Look first at the inner summation.

sum(2*a+b,b=0..999)

As the two terms do not both depend upon b, this can be separated into
two summations,

sum(2*a,b=0..999) + sum(b,b=0..999)

The first of this is of course 2*a*(999-0+1) which is 2*a*1000 = 2000*a
The second of these is the same as sum(b,b=1..999), which by the classic
formula is bmax * (bmax+1) / 2, which would be 999 * 1000 / 2 which
would be 499500. Thus, the inner summation is 2000*a + 499500

Now consider the outer summation over a:

sum(2000*a + 499500,a=0..999)

Again, this is separable

sum(2000*a,a=0..999) + sum(499500,a=0..999)

Now, sum of a constant times the variable is equal to the constant times
the sum, so this becomes

2000 * sum(a,a=0..999) + sum(499500,a=0..999)

And by the classic formula again, the first of those sums itself will be
the same as the separate sum we came up with for b, 499500, and the
second will be the constant * (999-0+1) which is 1000 * the constant. We
thus get

2000 * 499500 + 1000* 499500

which is easily calculated by Matlab without using a for loop.

Based upon this, you should be able to easily find a formula for the
more general cases where the summations are to other limits.