How to do it?
4 views (last 30 days)
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.
Image Analyst on 11 Jul 2017
Hints. Look at functions size() (to get the size of the matrix), and sum(), tril(), triu(), flipud(), and/or flidlr(). Or simply use a nested for loop for a brute force method.
Srishti Saha on 7 Apr 2018
This code works perfectly for me:
%function to compute sum of lower most right side triangle in an X*2 matrix
function u = halfsum(P)
u1 = P(end:-1:1, 1:end);
u2 = triu(u1);
u = sum(u2(:));