# [MATLAB] - Writing sums in MATLAB

#### TheNewbie

##### New Member
Hello, I am trying to figure out how to code this by using loops in MATLAB. If someone has a better idea, please share it.

$$\sum_{i=1}^n A_i + 3\sum_{i \neq j}^n B_iC_j + 6\sum_{i>j>k}^n D_iE_jF_k$$

It looks like the number of terms in the expression above is given by:
$${n \choose 1} + 3\times 2 \times {n \choose 2} + 6{n \choose 3}$$
$$= n + 3n(n - 1) + n(n - 1)(n - 2) = n + 3n^2 - 3n + n^3 - 3n^2 + 2n = n^3$$

For the first part of the expression above, I just wrote:

FirstExpression=sum(A); %Comment: Since A,B,C etc all are 1 x n vectors

Last edited:

#### fed1

##### TS Contributor
Re: Writing sums in MATLAB

if you take B = <B1, B2,...,Bn> and similar for C you can write

>> BC = B' * C ;
so BC is the matrix of products;