# How to do it?

15 views (last 30 days)

Show older comments

##### 8 Comments

Jorge Briceño
on 3 Feb 2018

Hi everyone,

I solved the problem in two ways.

First one, using two for loops.

function [counter] = halfsum (A)

% ii = rows ; jj = columns

[m,n]=size(A);

counter = 0;

col=1;

for ii=m:-1:1

for jj=col:n

counter=counter + A(ii,jj);

end

col=col+1; % This line will add a number to your column value,

% so you will not start in the first column again.

end

counter;

end

Second one, using a for loop and an if statement.

function [counter] = halfsum (A)

% ii = rows ; jj = columns

[m,n]=size(A);

counter = 0;

ii=m;

for jj=1:n

if ii>0 % This contidion is mandatory, otherwise the ii could be equal zero, which will result in an error

counter=counter + sum(A(ii,jj:n)); % Function sum(A(ii,jj:n)) sums all the column in a row.

ii=ii-1; % This is equivalent to a row for loop.

end

end

counter;

end

I hope it helps.

Cheers!

### Answers (3)

Image Analyst
on 11 Jul 2017

##### 0 Comments

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(:));

end

##### 0 Comments

RAMAKANT SHAKYA
on 7 Feb 2019

Edited: RAMAKANT SHAKYA
on 8 Feb 2019

function s=halfsum(a)

[m,n]=size(a);

s=0;

a=flip(a);

for r=1:m

for c=n:-1:r

s=s+a(r,c);

end

end

end

##### 2 Comments

Image Analyst
on 8 Feb 2019

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!