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
정보
이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.
이전 댓글 표시
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
nikhil singh
2020년 4월 26일
This program is for calculating sum of upper triangle of a Matrix, hope it helps
function summa=halfsum(M)
[row column]= size(M);
summa=0;
sumdiagonal=0;
sumupper=0;
for i=1:row
for j=1:column
if i==j
sumdiagonal = sum(M(i,j))+ sumdiagonal;
end
if i<j
sumupper = sum(M(i,j)) + sumupper;
end
summa=sumdiagonal + sumupper;
end
end
end
Walter Roberson
2020년 4월 26일
Is there a particular reason to sum the diagonal seperately from summing the upper part?
Soham Bhosale
2020년 6월 2일
i think this program will help you for summing upper triangular part
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;
TINOY SANTRA
2020년 6월 13일
function summa=halfsum(A)
b=size(A);
row=b(1);
col=b(2);
summa=0;
for i=1:row
for j=1:col
if(i==j)
summa=summa+sum(A(i,j:end));
end
end
end
summa
TINOY SANTRA
2020년 6월 13일
i did this code and it worked fine
Image Analyst
2020년 6월 13일
Do not post up here. If you think that none of the 18 answers below and 2 answers above answer the question and that your answer offers something unique, then you can reopen the question to post your answer that is different than the prior 20 solutions.
채택된 답변
추가 답변 (17개)
Vikrant Prasad
2019년 3월 22일
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
asad jaffar
2019년 3월 31일
vikrant how to write it for uperright instead of lower right ?
Image Analyst
2019년 3월 31일
편집: Image Analyst
2019년 3월 31일
Did you try
if i>=j
and it didn't work??? Or did you do
summa = sum(A(triu(A)))
You didn't use sum for the name of the variable, contrary to what I said in my answer, did you??
Please attach the code so we can check why it's not working.
mayank ghugretkar
2019년 6월 10일
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
2017년 5월 25일
function u = halfsum(P)
u1 = P(end:-1:1, 1:end);
u2 = triu(u1);
u = sum(u2(:));
end
댓글 수: 3
Jorge Briceño
2018년 2월 3일
Hi everyone,
I solve the problem in a different way. Here it is:
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
Another way using rows and columns for loops would look like:
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;
end
counter;
end
William Gallego
2018년 11월 3일
편집: William Gallego
2018년 11월 3일
Great, I understood both ways. Actually, I spent most of my time trying to figure out the loop for ii=m:-1:1. I forgot to count backward :(. Thanks for the help!
Mukti Awad
2019년 8월 16일
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
2019년 6월 26일
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
Image Analyst
2016년 9월 7일
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
Justin Whetten
2016년 12월 13일
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
Steven Lord
2016년 12월 13일
There's no need for a test inside the loops.
In row 1, how many elements will be included in the sum and in which column are those elements located?
In row 2, how many elements will be included in the sum and in which column are those elements located?
Continue asking this question and see if there is a pattern to the answers that you can exploit in the definition of the second for loop to force only those elements that should be included in the sum to be included.
Image Analyst
2016년 12월 14일
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
2019년 6월 25일
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
Roshan Singh
2019년 9월 15일
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
Walter Roberson
2019년 9월 15일
(x==y)||(x<y) is the same logical condition as x<=y but x<=y is more efficient.
Sebastián Añazco
2019년 9월 21일
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
Shubham Pandey
2020년 4월 3일
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
Shubham Pandey
2020년 4월 3일
this works fine
Image Analyst
2020년 4월 3일
But every MATLAB expert would advise anyone to never use the name of a built-in function like sum() as the name of a variable.
Shubham Pandey
2020년 4월 3일
I agree with your feedback. Just a noobie mistake.
Nitin Kumar soni
2020년 4월 27일
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
Walter Roberson
2020년 4월 28일
You can avoid the if statement if you have j end at i instead of continuing to col.
Ashay Nagdive
2020년 5월 1일
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
SANTOSH KAMBLE
2020년 5월 4일
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
ADARSH VISAJI
2020년 5월 9일
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
Image Analyst
2020년 5월 9일
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
2020년 5월 15일
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
vighnesh rana
2020년 5월 19일
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
Geetanjali Alle
2020년 6월 5일
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
Walter Roberson
2020년 6월 8일
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
2020년 6월 11일
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
이 질문은 마감되었습니다.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!