Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

WHY IT IS WRONG?

1 Ansicht (letzte 30 Tage)
SAYANTAN BHANJA
SAYANTAN BHANJA am 15 Jul. 2017
Geschlossen: James Tursa am 15 Jul. 2017
Write a function called halfsum that takes as input an at most two-dimensional array A and computes the sum of the elements of A that are in the lower right triangular part of A, that is, elements in the counter-diagonal (going from the bottom left corner, up and to the right) and elements that are to the right of it. For example, if the input is [1 2; 3 4; 5 6; 7 8], then the function would return 21.
function [H,r,c]=halfsum(x)
[r,c]=size(x);
if(c==2)
k=(r-2);
d=x';
z=flipud(d);
S=triu(z,k);
H=sum(S(:))
end
if(r==2)
k=(c-2);
z=flipud(x);
S=triu(z,k);
H=sum(S(:))
end
if(r>=c&&c~=2)
k=(r-3);
d=x';
z=flipud(d);
S=triu(z,k);
H=sum(S(:))
end
if(c>r&&r~=2)
k=(c-3);
z=flipud(x);
S=triu(z,k);
H=sum(S(:))
end
WHAT IS WRONG IN IT? GIVING WRONG RESULT FOR X=[1 2 3 4;4 5 6 8]

Antworten (0)

Diese Frage ist geschlossen.

Community Treasure Hunt

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

Start Hunting!

Translated by