How to sum up a member of a 3D struct for all elements?

조회 수: 1 (최근 30일)
sim_sup
sim_sup 2023년 2월 12일
편집: Askic V 2023년 2월 12일
Dear Community,
I have a 3D struct, with several members. I would like to sum up a certain member (oipsc) of the struct for all elements. How I could do it? I've tried several methosds, but frankly I don't know the right command i.e. syntax. I'm relatively new to Matlab. I've attached a simple SumTest.m code to higlight my problem.
Tx for the kind help in advance,
best regards,
Andras

답변 (1개)

Askic V
Askic V 2023년 2월 12일
편집: Askic V 2023년 2월 12일
Do you really need this (implemented with loops):
[d1,d2,d3] = size(gridblock);
sum_oipsc = 0;
for i = 1:d1
for j = 1:d2
for k = 1:d3
sum_oipsc = sum_oipsc + gridblock(i,j,k).oipsc;
end
end
end
If that is the case, then the solution would be as simple as:
sum_oipsc2 = sum([gridblock(:).oipsc])
  댓글 수: 2
sim_sup
sim_sup 2023년 2월 12일
Dear Askic,
This was exactly the answer I was looking for :-)
Tx very much,
best regards
Andras
Askic V
Askic V 2023년 2월 12일
편집: Askic V 2023년 2월 12일
@sim_sup, In that case, please consider accepting this as an asnwer, so it can benefit others in the future.

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

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by