how to create n*n upper triangular matrix with conditions? (image attached)

 Accepted Answer

n = 4;
[ii,jj] = ndgrid(1:n);
out = (ii == jj) - (ii < jj);
or
n = 4;
out = eye(n) - triu(ones(n),1);

4 Comments

thanks for your help but if we want some random number or use "magic" then what we do? example : i want a n*n matrix of some numbers.. IS IT POSSIBLE?
forget it.... If we use conditional statement in this then what is the command in editor?

Sign in to comment.

More Answers (1)

m = input ('m=');
n= input('n=')
a=zeros(m,n);
for i=1:m
fprintf('Linia %g : ', i);
a ( i, :)= input ( ' ' ) ;
end
for i=1:m
for j=1:n
if i < j
a (i,j) = 0
end
end
end
fprintf(' Matrice superior triunghiulara ' )

Categories

Community Treasure Hunt

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

Start Hunting!