필터 지우기
필터 지우기

How can I get the upper triangular matrix without using triu()?

조회 수: 17 (최근 30일)
Jessica Avellaneda
Jessica Avellaneda 2022년 2월 24일
댓글: Voss 2022년 2월 27일
I was able to get the lower triangular matrix by using A(:, 1:end-1), A being my function + argumented matrix but I need to get the upper triangular matrix.
The matrix is Aug= a matrix 3x3, b= 1x3. (A = [Aug b])
  댓글 수: 3
Jessica Avellaneda
Jessica Avellaneda 2022년 2월 27일
Yes, you are correct. However, I cannot use tril or triu for this assignment. is there any other way I can get lower and upper triangular matrices ?
Voss
Voss 2022년 2월 27일
@Jessica Avellaneda Please see my answer, which does not use triu() except to show that the result is correct, which is a part you can omit.

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

답변 (1개)

Voss
Voss 2022년 2월 25일
편집: Voss 2022년 2월 27일
You can set all elements below the main diagonal to zero, e.g., using a for loop:
% A matrix, pick any size you want:
A = randn(5,6);
disp(A);
-0.2986 -0.0896 3.0956 -0.0877 -0.1211 -0.8411 -1.2512 -0.7189 -0.2814 1.0478 -0.5774 1.0418 0.3396 0.8059 1.3927 0.8229 -0.7386 -0.3184 0.4703 0.3028 0.7384 -0.1581 0.4213 0.2998 -0.4320 1.3279 -2.2420 -1.0304 0.4372 0.4579
% zero out the elements below the diagonal to make it upper-triangular:
A_ut = A;
for ii = 1:size(A,2)
A_ut(ii+1:end,ii) = 0;
end
disp(A_ut);
-0.2986 -0.0896 3.0956 -0.0877 -0.1211 -0.8411 0 -0.7189 -0.2814 1.0478 -0.5774 1.0418 0 0 1.3927 0.8229 -0.7386 -0.3184 0 0 0 -0.1581 0.4213 0.2998 0 0 0 0 0.4372 0.4579
% make sure this gives the same result as triu(A):
isequal(A_ut,triu(A))
ans = logical
1

카테고리

Help CenterFile Exchange에서 Sparse Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by