필터 지우기
필터 지우기

How to sum the results of a loop?

조회 수: 1 (최근 30일)
vinicius lanziotti
vinicius lanziotti 2017년 12월 7일
편집: James Tursa 2017년 12월 7일
I need to sum the values of D and put the result in a single variable...
How can I do this?
x=[1 2 3 4 5];
d= [0 10 20 30 40;
10 0 50 60 70;
20 50 0 80 90;
30 60 80 0 100;
40 70 90 100 0];
for i=1:length(x)-1
j=i+1;
D=d(x(1,i),x(1,j))
end
>> test
D =
10
D =
50
D =
80
D =
100

답변 (1개)

James Tursa
James Tursa 2017년 12월 7일
편집: James Tursa 2017년 12월 7일
E.g., for generic x
s = sub2ind(size(d),x(1:end-1),x(2:end));
Dsum = sum(d(s));
If you always wanted to sum the super-diagonal, then
Dsum = sum(diag(d,1));

카테고리

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

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by