Problem 413. Back to basics 23 - Triangular matrix
Covering some basic topics I haven't seen elsewhere on Cody.
Given an input matrix, return a matrix with all elements above and to the right of the main diagonal set to zero. Example:
input = [ 1 1 1 1; 1 1 1 1; 1 1 1 1; 1 1 1 1] output = [1 0 0 0; 1 1 0 0; 1 1 1 0; 1 1 1 1]
Solution Stats
Problem Comments
-
3 Comments
Your test suite doesn't really match the problem statement. The tests require matrices with *exactly* all elements abot the main diagonal to be 0 *and* all elements on or below the main diagonal to be 1.
Additional test cases have been added.
Running this on octave returns the correct results yet the page still says I'm wrong.
function y = triangMatrix(x)
for i=1:rows(x);
for j=1:columns(x);
if j>i;
x(i,j)=0;
else
endif
endfor
endfor
disp(x)
endfunction
Solution Comments
Show commentsProblem Recent Solvers1042
Suggested Problems
-
Return the largest number that is adjacent to a zero
5414 Solvers
-
Remove the polynomials that have positive real elements of their roots.
1705 Solvers
-
Project Euler: Problem 7, Nth prime
1569 Solvers
-
Create an n-by-n null matrix and fill with ones certain positions
635 Solvers
-
ベクトル [1 2 3 4 5 6 7 8 9 10] の作成
566 Solvers
More from this Author39
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!