Matlab tril function does not work as expected

조회 수: 3 (최근 30일)
Yuzhen Lu
Yuzhen Lu 2020년 4월 12일
답변: Image Analyst 2020년 4월 13일
I am using Matlab2019a. When I was using tril an triu functions for generating triangular matrix, they ony give single values. Here are their function descriptions when type editing commdands:
tril Extract lower triangular part.
tril(X) is the lower triangular part of X.
tril(X,K) is the elements on and below the K-th diagonal
of X . K = 0 is the main diagonal, K > 0 is above the
main diagonal and K < 0 is below the main diagonal.
triu Extract upper triangular part.
triu(X) is the upper triangular part of X.
triu(X,K) is the elements on and above the K-th diagonal of
X. K = 0 is the main diagonal, K > 0 is above the main
diagonal and K < 0 is below the main diagonal.
When typing a simple command:
tril(4)
it gives a single value 4
Why the version of Matlab does not support the offiicially claimed functionalities (both introduced since 2006a) as described at https://www.mathworks.com/help/matlab/ref/triu.html and https://www.mathworks.com/help/matlab/ref/tril.html
  댓글 수: 1
James Tursa
James Tursa 2020년 4월 12일
You are going to have to provide examples of why you think they don't work rather that just making a blanket statement.

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

채택된 답변

Image Analyst
Image Analyst 2020년 4월 13일
4 is a scalar (or a matrix of 1 row and 1 column) - essentially there is no upper or lower triangular part. There is only one part - one element: 4. So it returns 4, as expected.
So now let's look at an actual matrix (larger than a 1-by-1):
m = magic(7)
t = tril(m)
What does this return for you? For me it returns:
m =
30 39 48 1 10 19 28
38 47 7 9 18 27 29
46 6 8 17 26 35 37
5 14 16 25 34 36 45
13 15 24 33 42 44 4
21 23 32 41 43 3 12
22 31 40 49 2 11 20
t =
30 0 0 0 0 0 0
38 47 0 0 0 0 0
46 6 8 0 0 0 0
5 14 16 25 0 0 0
13 15 24 33 42 0 0
21 23 32 41 43 3 0
22 31 40 49 2 11 20
again, exactly as expected. So now, what does it return for you (evidently it's wrong you say so you won't get what I got) in R2019a, and in the earlier version you used? Sometimes new functions change behavior you know. What was the earlier version you used?

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Linear Algebra에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by