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.

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

请先登录,再进行评论。

采纳的回答

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