File Exchange

image thumbnail

Sum of array's elements in the lower right triangular part

version 1.0.1 (1.25 KB) by Ibrahim Abouemira
The function takes as input at the most 2-D array. It computes the sum of the elements that are in the lower right triangular part

2 Downloads

Updated 19 May 2019

View License

The function takes as input at the most two-dimensional array. It computes the sum of the elements of that are in the lower right triangular part(counter-diagonal elements).
For example, if the input is [1 2; 3 4; 5 6; 7 8], then the function would return 21.

Cite As

Ibrahim Abouemira (2020). Sum of array's elements in the lower right triangular part (https://www.mathworks.com/matlabcentral/fileexchange/71592-sum-of-array-s-elements-in-the-lower-right-triangular-part), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (1)

Stephen Cobeldick

Rather complex code for such a simple task. Some logical indexing does the same thing (requires R2016b or later, or BSXFUN):

>> A = [1,2;3,4;5,6;7,8]
A =
1 2
3 4
5 6
7 8
>> S = size(A);
>> sum(A((S(1)-(1:S(1)).'-(1:S(2))<0)))
ans = 21
>> halfsum(A)
ans = 21

>> A = [1,2,3,4,5;6,7,8,9,0]
A =
1 2 3 4 5
6 7 8 9 0
>> S = size(A);
>> sum(A((S(1)-(1:S(1)).'-(1:S(2))<0)))
ans = 44
>> halfsum(A)
ans = 44

Updates

1.0.1

Name changed.

MATLAB Release Compatibility
Created with R2017a
Compatible with any release
Platform Compatibility
Windows macOS Linux