필터 지우기
필터 지우기

How do I create a simple loop to sum up the elements in my matrix assuming some elements vary.

조회 수: 2 (최근 30일)
Tp1 = [ E*A1 E*Sx1 E*Sy1 E*Sw1 ; E*Sx1 E*Ix1 E*Ixy E*Iwy; E*Sy1 E*Ixy E*Iy1 E*Iwx ];
To1 = [ 0 0 0 0; 0 0 0 0; 0 0 0 0] ;
Kt1=repmat({Tp1},128,128);
Kt1([28,48,50,124])={To1};
Kt1=cell2mat(Kt1);

채택된 답변

Dave B
Dave B 2021년 11월 13일
You don't need a loop:
a = rand(3,4)
a = 3×4
0.2467 0.9178 0.3863 0.5492 0.8022 0.1494 0.9996 0.6980 0.3715 0.9611 0.1072 0.4959
sum(a) % sum of each column, also sum(a,1)
ans = 1×4
1.4204 2.0283 1.4931 1.7431
sum(a,2) % sum of each row
ans = 3×1
2.1000 2.6492 1.9356
sum(a,'all') % sum of all values, or sum(a(:)) on old releases
ans = 6.6848
If you really want to do it in a loop:
s=0;
for i = 1:numel(a)
s=s+a(i);
end
s
s = 6.6848

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by