MATLAB Answers

0

I have a 300 x 150 zeros matrix and want to change only the lower triangular elements to 1, How do I do that?

Asked by Daniel Gray on 3 Jul 2017
Latest activity Edited by Jan
on 3 Jul 2017
Everything is in the title really, I need to change the lower triangular elements of this zeros matrix to 1, how can I do this easily?

3 Answers

Answer by Image Analyst
on 3 Jul 2017
 Accepted Answer

You don't even need your original zeros matrix (let's say it's called z). Simply make a 15x30 matrix of zeros with the lower left triangle of 1's like this:
z = [tril(ones(15), 0), zeros(15)]

  0 Comments

Sign in to comment.


Answer by KSSV
on 3 Jul 2017
Edited by KSSV
on 3 Jul 2017

idx = ones(300,150) ;
idx = tril(idx,1) ; % Make 1's at lower triangular part
A = rand(300,150) ; % given matrix
A(idx==1) = 1 ; % replace lower triangular part with 1

  0 Comments

Sign in to comment.


Answer by Jan
on 3 Jul 2017
Edited by Jan
on 3 Jul 2017

In Matlb >= R2016b:
M = (1:300).' >= (1:150);
For older Matlab versions:
M = bsxfun(@ge, (1:300).', 1:150);

  0 Comments

Sign in to comment.