Write a function that returns triangular part of matrix using loops

I just started to learn how to program using matlab and in class we have covered if and for loops but I am really not sure how to solve this problem. Can someone please help?
Write a function that inputs a square matrix and returns
another matrix which is identical to the first, except that the elements below the diagonal are
set to zero.
Your function should use nested for loops. If given a matrix:
4 3 4 4
4 1 4 2
1 2 1 4
4 3 4 1
It should return the following matrix:
4 3 4 4
0 1 4 2
0 0 1 4
0 0 0 1
Show your output for an illustrative 5x5 matrix input.

댓글 수: 1

Jan
Jan 2013년 9월 26일
편집: Jan 2013년 9월 26일
Because this is a homework, it is your turn to show us, what you have tried so far and explain which problems occur. Tip: You can solve it by two FOR loops and compare the indices.

댓글을 달려면 로그인하십시오.

답변 (2개)

Matt J
Matt J 2013년 9월 26일
편집: Matt J 2013년 9월 26일
Probably not what you're looking for, but course instructors are so dumb sometimes...
function B=uppertri(A)
for i=1
for j=1
B=triu(A);
end
end
Friday N. Abolorunke
Friday N. Abolorunke 2020년 6월 8일
편집: Friday N. Abolorunke 2020년 6월 8일
function summa = halfsum(A)
[m,n] = size(A);
for ii = 1:m
for jj = 1:n
if ii > jj
A(ii,jj) = 0
summa = sum(A(:));
elseif ii == 1
summa = sum(A);
end
end
end
end
For a 3 x 3 matrix. you'll want to assign zero to A(2,1),A(3,1) and A(3,2). Notice that the index of the elements in the row most always be greater than that of the col

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품

질문:

2013년 9월 26일

편집:

2020년 6월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by