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.

8 comentarios

Rik
Rik el 11 de Jul. de 2017
This looks like homework. You can find guidelines for posting homework on this forum here.
Make sure to show what you tried already. If your question gets tagged doit4me, your chances on an answer decrease a lot.
SAYANTAN BHANJA
SAYANTAN BHANJA el 15 de Jul. de 2017
function [H,r,c]=halfsum(x)
[r,c]=size(x);
if(c==2)
k=(r-2);
d=x';
z=flipud(d);
S=triu(z,k);
H=sum(S(:))
end
if(r==2)
k=(c-2);
z=flipud(x);
S=triu(z,k);
H=sum(S(:))
end
if(r>=c&&c~=2)
k=(r-3);
d=x';
z=flipud(d);
S=triu(z,k);
H=sum(S(:))
end
if(c>r&&r~=2)
k=(c-3);
z=flipud(x);
S=triu(z,k);
H=sum(S(:))
end
WHAT IS WRONG IN IT? GIVING WRONG RESULT FOR X=[1 2 3 4;4 5 6 8]
James Tursa
James Tursa el 15 de Jul. de 2017
Editada: James Tursa el 15 de Jul. de 2017
The fact that you are testing for the explicit conditions of r==2 and c==2 leads me to believe that you are interpreting this condition incorrectly:
"... at most two-dimensional array A ..."
That phrase simply means that A has at most two dimensions. I.e., A is either a row vector or a column vector or a 2D matrix. It can't be a 3D or 4D etc array. E.g. the variable A could be 1x9 or 22x1 or 34x23, but it cannot be 3x4x5 nor 4x8x2x7 etc. It doesn't mean that one of the dimension values must be 2.
Because of this, it appears to me that you are making this more complicated than you need to. E.g., at most you would only need to cover two cases in your code if you want something similar to the logic you have shown thus far:
if( r > c )
% code for this case here
else
% code for the r <= c case here
end
SAYANTAN BHANJA
SAYANTAN BHANJA el 15 de Jul. de 2017
can you please give the solution of it ? or any clue
James Tursa
James Tursa el 15 de Jul. de 2017
OK, let's make it simpler. 0-pad the incoming matrix to make it square, and then deal with the square matrix downstream in your code:
[r,c] = size(x);
if( r < c )
x = new matrix padded with 0's on top to make it square
else
x = new matrix padded with 0's on right to make it square
end
% then deal with the square matrix x downstream
So, first figure out how to do the padding. Then the downstream stuff you should be able to figure out using logic very similar to what you already have shown above.
SAYANTAN BHANJA
SAYANTAN BHANJA el 16 de Jul. de 2017
function [N,r,c]=halfsum(x)
[r,c]=size(x);
if(r>c)
col_size=size(x,1);
zero_row=zeros(col_size,(r-c));
H=[x,zero_row];
s=flipud(H);
d=triu(s);
N=sum(d(:));
end
if(c>r)
col_size=size(x,2);
zero_row=zeros((c-r),col_size);
H=[x;zero_row];
s=flipud(H);
d=triu(s);
N=sum(d(:));
end
if(r==c)
s=flipud(x);
d=triu(s);
N=sum(d(:));
end
end
AGAIN ERROR FOR X=[1 2 3 4;5 6 7 8] PLEASE TELL WHAT TO DO?
SAYANTAN BHANJA
SAYANTAN BHANJA el 16 de Jul. de 2017
function [N,r,c]=halfsum(x)
[r,c]=size(x);
if(r>c)
col_size=size(x,1);
zero_row=zeros(col_size,(r-c));
H=[x,zero_row];
s=flipud(H);
d=triu(s);
N=sum(d(:));
end
if(c>r)
col_size=size(x,2);
zero_row=zeros((c-r),col_size);
H=[zero_row;x];
s=flipud(H);
d=triu(s);
N=sum(d(:));
end
if(r==c)
s=flipud(x);
d=triu(s);
N=sum(d(:));
end
end
THANKS A LOT SIR GOT THIS ONE.........THANKS A LOT
Jorge Briceño
Jorge Briceño el 3 de Feb. de 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!

Iniciar sesión para comentar.

Respuestas (3)

Image Analyst
Image Analyst el 11 de Jul. de 2017

0 votos

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
Srishti Saha el 7 de Abr. de 2018

0 votos

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
RAMAKANT SHAKYA
RAMAKANT SHAKYA el 7 de Feb. de 2019
Editada: RAMAKANT SHAKYA el 8 de Feb. de 2019

0 votos

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 comentarios

Image Analyst
Image Analyst el 8 de Feb. de 2019
Do not use the built-in function sum() as the name of your variable. Call it theSum or something other than sum.
RAMAKANT SHAKYA
RAMAKANT SHAKYA el 8 de Feb. de 2019
thank you sir for improving me.

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Preguntada:

el 11 de Jul. de 2017

Comentada:

el 8 de Feb. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by