How can I get the upper triangular matrix without using triu()?
이전 댓글 표시
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
Um, what you claim to produce the lower triangle is not valid. For example:
A = randi(5,[3,4])
What you claim to have done, thus
A(:, 1:end-1)
That merely extracts the first three columns of A, which is not at all a lower triangular form. This is what tril and triu give:
tril(A)
triu(A)
Is that not what you want to produce? If not, then what do you really wish to see, and why are triu or tril not adequate for your purposes? We cannot really help you unless you are clear about what you are doing, and perhaps even why tril and triu are not of value.
Jessica Avellaneda
2022년 2월 27일
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개)
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);
% 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);
% make sure this gives the same result as triu(A):
isequal(A_ut,triu(A))
카테고리
도움말 센터 및 File Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!