but i am getting the error

Problem 3 (halfsum): Feedback: Your program made an error for argument(s) [1 2 3 4 5 6 7 8 9 10] \please help

216 views (last 30 days)

Show older comments

function s = halfsum(A)

[row col] = size(A);

if row ~= col

error('Expecting a square matrix here people...');

end

s = 0;

for ii = 1:row

for jj = ii:col

s = s + A(ii,jj);

end

end

Buwaneka Dissanayake
on 21 Jun 2020

function summa = halfsum(M)

[a b] = size(M);

if a>1

for n = 1:a;

for m = 1:b;

if n>m;

M(n,m) = 0;

summa = sum(sum(M));

end

end

end

else

summa = sum(M);

end

end

DGM
on 25 Jul 2022

Edited: DGM
on 25 Jul 2022

Of all the inefficient loop-based approaches on this page, this appears to be the most wasteful by far. For every single element of the array, you calculate the sum of the entire array and then discard the result. The only sum that doesn't get discarded is the last one. As a consequence, the time required grows rapidly as the matrix size increases -- all for nothing.

Try feeding this a large array. For a 400x400 matrix, this takes roughly 1000x as much time as the other loop-based examples, and nearly 6000x as much time as @Sean de Wolski's concise and efficient answer. Would you dare to feed it a 4000x4000 matrix?

Sean de Wolski
on 28 May 2015

f = @(x)sum(sum(triu(x))) % make function

f(magic(3)) % use it

Joseph Cheng
on 28 May 2015

your input should be

input = [1 2 3;4 5 6;7 8 9]

and not

input = [1 2 3 4 5 6 7 8 9]

Joseph Cheng
on 28 May 2015

Pragyan Dash
on 19 Sep 2020

%this worked for me. Happy to help!

function summa = halfsum(M)

[row col] = size(M)

summa = 0;

for ii = 1:row;

for jj = 1:col;

if jj >= ii;

summa = summa + M(ii, jj);

end

end

end

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

Ajith Bharadwaj
on 3 Feb 2020

function summa = halfsum(A)

[row col] = size(A);

for ii = 1:row

for jj = ii:col

summa = summa + A(ii,jj);

end

end

Amit Jain
on 3 Nov 2020

ERTIZA HOSSAIN SHOPNIL
on 21 May 2020

function summa=halfsum(A)

t=triu(A);

list=sum(t);

s=0;

for n=list

s=s+n;

end

summa=s;

end

Rik
on 24 Aug 2020

That means you will have to copy another answer to cheat on your homework. Or think yourself.

saurav Tiwari
on 25 Jun 2020

function summa=halfsum(a)

[m,n]=size(a)

for i=1:m

j=1:n

x(i)=sum(a(i,j))

end

summa=sum(x)

end

Walter Roberson
on 25 Jun 2020

youssef boudhaouia
on 27 Jul 2020

A solution with double For-loop:

function summa=halfsum(M)

summa=0;

s=size(M);

for i=1:s(1)

for j=1:s(2)

if j>=i

summa=summa+M(i,j);

else

summa=summa;

end

end

end

Abdul Quadir Khan
on 18 Oct 2020

function summa = halfsum(x)

[row,col] = size(x);

allsum=0;

for n=1:row

for c=n:col

allsum=x(n,c)+allsum;

end

end

summa=allsum;

Muhammad
on 25 Jul 2022

function summa=halfsum(M)

[row,col]=size(M);

total1=0;

total2=0;

for m=1:row

if m==1

for n=1:col

P=M(m,n)+total1;

total1=P;

end

elseif m>1 && m<=n

for n=m:col

Q= M(m,n)+total2;

total2=Q;

end

end

end

summa=total1+total2;

DGM
on 25 Jul 2022

Compared to the other loop-based approaches, what is the particular benefit of this one? It's not particularly fast or concise in comparison.

Posting reference answers is a good thing, but it's best to explain why your answer takes the form it has. This is particularly important when there are many similar answers in the same thread.

Maybe describe the problem-solving approach that brought you to the solution. Maybe you asserted certain goals or constraints that led you to solve it differently than others. Explain why a reader might want to follow your example -- or even the cases in which they might want to follow one of the others. It doesn't have to be much description, but something is better than nothing.

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

Start Hunting!