**Info**

This question is closed. Reopen it to edit or answer.

# 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

1 view (last 30 days)

Show older comments

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 9], then the function would return 38.

This is all I have so far:

function sum=halfsum(v)

sum=0;

[row col]=size(v);

for i= 1:row;

for j=1:col;

I have spent hours on trying to figure out how to sum the reverse diagonal starting at the bottom left element and everything to the right of it and cannot seem to figure it out. Any help is greatly appreciated!

##### 6 Comments

Image Analyst
on 13 Jun 2020

### Accepted Answer

James Tursa
on 7 Sep 2016

Edited: James Tursa
on 7 Sep 2016

Ask yourself, "what does it mean to be in the lower right triangle" in terms of i and j? I.e., what test could you make on i and j to determine if you were in the lower right triangle? Then code that up inside your for loops. E.g.,

mysum = 0; % <-- Changed sum to mysum since sum is the name of a MATLAB function

[row,col] = size(v);

for i= 1:row

for j=1:col

if( in lower right triangle ) % <-- you put test code inside parentheses

mysum = mysum + v(i,j);

end

end

end

So, you just need to figure out a test you can make on i and j to determine if you are in the lower right triangle, and insert that code inside the parentheses as noted above. If you can't figure this out immediately, write down all of the i,j pairs for a small matrix on a piece of paper and then look for a pattern that you can exploit and code up that isolates the lower right triangle. Do an example for both a square and non-square matrix to make sure the code you come up with works in both cases.

SIDE NOTE: There are ways to do this without a double for loop, but you should first learn how to do this with for loops.

##### 2 Comments

Image Analyst
on 21 Sep 2016

### More Answers (17)

Vikrant Prasad
on 22 Mar 2019

function summa = halfsum(A)

[row, col]=size(A);

sum=0;

for i=1:row

for j=i:col

if i<=j

sum= sum + A(i,j);

end

end

end

summa =sum;

end

##### 3 Comments

mayank ghugretkar
on 10 Jun 2019

another similar way

1. with for loop

function summa=halfsum(A)

[row col]=size(A);

summa=0;

j=0;

for i=1:row

for j=1:col

if j>=i

summa= A(i,j) +summa;

end

end

end

end

2. with While loop

function summa=halfsum(A)

[row col]=size(A);

summa=0;

for i=1:row

j=i;

while (j>=i && j<=col)

summa= A(i,j)+ summa;

j=j+1;

end

end

summa;

end

i love trying this things... simplifying n solving

Prashanth Reddy Penta Reddy
on 25 May 2017

function u = halfsum(P)

u1 = P(end:-1:1, 1:end);

u2 = triu(u1);

u = sum(u2(:));

end

##### 3 Comments

Mukti Awad
on 16 Aug 2019

I am getiing this error!!!

Assessment result: incorrect[1 2 3; 4 5 6; 7 8 9]

Variable summa has an incorrect value.

Nijita Kesavan Namboothiri
on 26 Jun 2019

function summa=halfsum(m)

summa=0

[r c]=size(m)

for i=1:r

for j=1:c

if i<=j

summa=summa+m(i,j)

end

end

end

##### 0 Comments

Image Analyst
on 7 Sep 2016

Try

theSum = 0; % DON'T USE sum as a variable name!!!

[rows, columns] = size(v);

for row = 1 : rows

for col = (columns - row + 1) : columns

theSum = theSum + .......

##### 0 Comments

Justin Whetten
on 13 Dec 2016

For anyone who might be struggling still with this

function sum=halfsum(A);

sum = 0;

[row,col] = size(A);

for i= 1:row

for j=1:col

if (i+j)>=(row+1)

sum = sum + A(i,j);

end

end

end

##### 2 Comments

Image Analyst
on 14 Dec 2016

And of course, NEVER use "sum" as the name of your variable (as I mentioned in my answer) since that's the name of a very important built-in function that you won't be able to use anymore if you redefine "sum" to be your own personal variable.

And if you're puzzled by what Steve said, just see my answer.

MANAV MALHOTRA
on 25 Jun 2019

function summa=halfsum(m)

summa=0;

for i=1:size(m,1)

for j=1:size(m,2)

if(i<=j)

summa=summa+sum(m(i,j));

end

end

end

for those who are less familiar with matlab

##### 0 Comments

Roshan Singh
on 15 Sep 2019

function summa=halfsum(a)

[r,c]=size(a);

summa=0;

for x=1:r

for y=1:c

if (x==y)||(x<y)

summa=a(x,y)+summa;

end

end

end

fprintf('summation of right hand matrix is...%d\n',summa)

end

##### 1 Comment

Walter Roberson
on 15 Sep 2019

(x==y)||(x<y) is the same logical condition as x<=y but x<=y is more efficient.

Sebastián Añazco
on 21 Sep 2019

function summa = halfsum(A)

summa = 0;

for n = 1 : size (A,1)

for m = 1:size(A,2)

if n >m

a = 0;

else

a = A(n, m);

end

summa = summa +a;

end

end

##### 0 Comments

Shubham Pandey
on 3 Apr 2020

function sum = halfsum(a)

sum = 0;

[row,col] = size(a);

for i=1:row

for j=1:col

if i<=j

sum=sum+a(i,j);

end

end

end

end

##### 3 Comments

Nitin Kumar soni
on 27 Apr 2020

function summa=halfsum(n)

[row,col]=size(n);

summa=0;

for i=1:row

for j=1:col

if i<=j

summa=summa+n(i,j);

end

end

end

##### 1 Comment

Walter Roberson
on 28 Apr 2020

You can avoid the if statement if you have j end at i instead of continuing to col.

Ashay Nagdive
on 1 May 2020

function a = halfsum(A)

[row col]= size(A);

a = 0;

for r= 1: row

for c= 1: col

if c >= r

a= a + A(r,c)

else

A(r,c)= 0;

end

end

end

##### 0 Comments

SANTOSH KAMBLE
on 4 May 2020

function summa=halfsum(x)

[row col]=size(x);

summa=0;

for r=1:row

for c=1:col

y=x(r,c);

if r<=c

summa=summa+y;

end

end

end

end

##### 0 Comments

ADARSH VISAJI
on 9 May 2020

function summa = halfsum(A)

[row, col]=size(A);

sum=0;

for i=1:row

for j=i:col

if i<=j

sum= sum + A(i,j);

end

end

end

summa =sum;

end

if u guys have any dobts on matlab problem u can contact me through instagram

my insta id:- visaji_adarsh

##### 1 Comment

Image Analyst
on 9 May 2020

This is similar to most of the other answers however you used the function name "sum" as a variable. It is not recommended to overwrite built-in functions with variables because then you can never use the built in function name anymore within that scope. For example if you wanted to do

theSum = sum(1:100);

you would not be able to because you made sum a scalar number -- it's not the built-in function anymore.

Taif Ahmed BIpul
on 15 May 2020

function summa=halfsum(A)

[m,n]=size(A);

summ=0;

for i=1:m

for ii=i:n

summ=summ+A(i,ii);

end

end

summa=summ;

##### 0 Comments

vighnesh rana
on 19 May 2020

function summa = halfsum(A)

[row col] = size(A);

total = 0;

for r = 1:row

for c= r:col

total = total + A(r,c);

end

end

summa = total;

end

##### 0 Comments

Geetanjali Alle
on 5 Jun 2020

function summa = halfsum(M)

[row column]=size(M);

summa=0;

for i=1:row

for j=1:column

if i<j||i==j

summa=summa + sum(M(i,j));

end

end

end

Hope this helps!

##### 1 Comment

Walter Roberson
on 8 Jun 2020

i<j||i==j

would be more efficient as

i<=j

There is also an easy change to prevent you from needing the if at all.

sum(M(i,j))

M(i,j) is a scalar. sum() of a scalar is the same scalar. You do not need sum() there.

AYUSH MISHRA
on 11 Jun 2020

function summa=halfsum(A)

[m,n]=size(A);

summa=0;

for i =1:m

fprintf('active row :%d \n',i);

for j=1:n

fprintf('active collumn :%d \n',j);

if m<=n

summa=summa+A(i,j);

end

fprintf('summa aeter every loop complition : %d \n',summa);

end

end

run it and understand every step working in loop

##### 0 Comments

### See Also

### Community Treasure Hunt

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

Start Hunting!