How the implement a 3-fold sum?

조회 수: 1 (최근 30일)
Stephan
Stephan 2017년 1월 21일
답변: Stephan 2017년 1월 23일
Hello,
there are answers for implementing a double sum. But what about a 3-fold sum of the type \sum (x_i*y_j*z_k)*M_{i,j,k} for some given vectors x,y,z and Tensor M.
A naiv implementation would be
SUM=0;
for i=1:length(x)
for j=1:length(y)
for k=1:length(z)
SUM = SUM + x(i)*y(j)*z(k)*M(i,j,k);
end
end
end
How to implement this more efficiently in a more compact form?
Thanks for any help!

채택된 답변

David Goodmanson
David Goodmanson 2017년 1월 22일
Hello Stephen, Here is one way, although you do make three arrays of the same size as M:
[xx yy zz] = ndgrid(x,y,z);
S = xx.*yy.*zz.*m;
SUM = sum(S(:))

추가 답변 (1개)

Stephan
Stephan 2017년 1월 23일
Thanks!

카테고리

Help CenterFile Exchange에서 Read, Write, and Modify Image에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by