I have to find the sum of the elements in the upper dimension matrix and this is the code i came up with. When I submitted this code it's showing error.

조회 수: 1 (최근 30일)
function summa=halfsum(A)
n=length(A);
k=1;
[m,p]=size(A);
if m~=p
return
else
summa=0;
for i=1:n
for j=k:n
summa=summa+A(i,j);
end
k=k+1;
end
end
  댓글 수: 2
MANOGNA POTLURI
MANOGNA POTLURI 2020년 7월 14일
function summa=halfsum(A)
summa=0;
n=length(A);
k=1;
[m,p]=size(A);
if m~=p
if p>m
A(p,p)=0;
elseif m>p
A(m,m)=0;
end
end
for i=1:n
for j=k:n
summa=summa+A(i,j);
end
k=k+1;
end
end
MANOGNA POTLURI
MANOGNA POTLURI 2020년 7월 14일
i got it now; the prof also wants to convert a non-square matrix to a square matrix and then find the halfsum.

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

채택된 답변

David Hill
David Hill 2020년 7월 14일
Why not just:
[a,b]=size(A);
if a==b
summa=sum(triu(A),'all');
end

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by