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

1 次查看(过去 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