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

36 vues (au cours des 30 derniers jours)
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?

Réponse acceptée

Image Analyst
Image Analyst le 3 Juil 2017
Modifié(e) : Image Analyst le 17 Mar 2024 à 14:24
You don't even need your original zeros matrix (let's say you called your original matrix "z") since it gets completely overwritten by 0's and 1's.
Simply make a 300 x 150 matrix of zeros with the lower left triangle of 1's like this:
z = [zeros(150); tril(ones(150), 0)];
% Now verify and display it.
size(z)
ans = 1×2
300 150
imshow(z)
axis('on', 'image')

Plus de réponses (3)

KSSV
KSSV le 3 Juil 2017
Modifié(e) : KSSV le 3 Juil 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

Jan
Jan le 3 Juil 2017
Modifié(e) : Jan le 3 Juil 2017
In Matlb >= R2016b:
M = (1:300).' >= (1:150);
For older Matlab versions:
M = bsxfun(@ge, (1:300).', 1:150);

Abdurayimov Orzumurod
Abdurayimov Orzumurod le 17 Mar 2024 à 10:55
idx = bir (300,150);
idx = tril(idx,1) ; % Pastki uchburchak qismiga 1 qo'ying
A = rand (300,150); % berilgan matritsa
A(idx==1) = 1 ; % pastki uchburchak qismini 1 bilan almashtiring

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by