Francesco Bonizzi
Francesco Bonizzi

Reputation: 5302

How can I compute this sum without a for loop?

In particular I'm interested in the summatory. It uses k two times, but using sum I don't know how to obtain the index.

Considering only the summatory:

summatory = sum( L(i, 1:j-1) * L(j, 1:j-1) );

is obviosly wrong.

How can I do it without a for loop?

My problem

Upvotes: 0

Views: 69

Answers (2)

chappjc
chappjc

Reputation: 30589

Either compute the inner product with vector algebra (i.e. v*v' as demonstrated by @BenVoigt), or use sum, but with the element-wise product (.*):

summatory = sum( L(i, 1:j-1) .* L(j, 1:j-1) );

Upvotes: 3

Ben Voigt
Ben Voigt

Reputation: 283893

That's an inner product between an 1x(j-1) vector and a (j-1)x1 vector:

krange = 1:j-1;
summatory = L(i, krange) * L(j, krange)';

Your code would also have worked (now that you've fixed the syntax), if you used the element-wise product operator .* instead of the matrix product *.

Upvotes: 3

Related Questions