Matlab tril function does not work as expected

4 visualizaciones (últimos 30 días)
Yuzhen Lu
Yuzhen Lu el 12 de Abr. de 2020
Respondida: Image Analyst el 13 de Abr. de 2020
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 comentario
James Tursa
James Tursa el 12 de Abr. de 2020
You are going to have to provide examples of why you think they don't work rather that just making a blanket statement.

Iniciar sesión para comentar.

Respuesta aceptada

Image Analyst
Image Analyst el 13 de Abr. de 2020
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?

Más respuestas (0)

Categorías

Más información sobre Operating on Diagonal Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by