Sum of a portion of 3D matrix elements

조회 수: 5 (최근 30일)
Dmitrii Lukashevich
Dmitrii Lukashevich 2021년 4월 8일
댓글: Dmitrii Lukashevich 2021년 4월 8일
Hello,
I would like to find a sum of 3D matrix elements located in a one tetrahedron. For a matrix A with indices (i, j, k) I need to find a sum of elements with indices (i, j > i, k > j), i.e.
total = 0;
for i = 1:N
for j = i:N
for k = j:N
total = total + A(i,j,k);
Is there a way to do it in a vector/matrix manner without loops?
"Tril/triu" function doesn't work with 3D matricies.
Thanks in advance!

채택된 답변

Matt J
Matt J 2021년 4월 8일
편집: Matt J 2021년 4월 8일
[I,J,K]=ndgrid(1:N);
total = sum(A(I<=J & J<=K))
  댓글 수: 2
Matt J
Matt J 2021년 4월 8일
You can conserve some memory by using ndgridVecs instead ( Download )
[I,J,K]=ndgridVecs(1:N);
total = sum(A(I<=J & J<=K))
Dmitrii Lukashevich
Dmitrii Lukashevich 2021년 4월 8일
Thank you a lot

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by