upmat, upmat1#
Purpose#
Returns the upper portion of a matrix. upmat()
returns the main diagonal and every element above.
upmat1()
is the same except it replaces the main diagonal with ones.
Format#
- u = upmat(x[, k, vector])#
- u = upmat1(x)#
- Parameters:
x (NxK matrix) – data
k (scalar) – Optional input, an offset for the start of the upper diagonal. See examples.
vector (scalar) – A binary input with a 1 indicating that the return should be a column vector containing only the upper triangular elements, or a zero indicating the return should be the standard matrix return.
- Returns:
u (NxK matrix) – containing the upper elements of x. The lower elements are replaced with zeros.
upmat()
returns the main diagonal intact.upmat1()
replaces the main diagonal with ones.
Examples#
Basic example#
x = { 7 2 -1,
2 3 -2,
4 -2 8 };
u = upmat(x);
u1 = upmat1(x);
The resulting matrices are:
7 2 -1 1 2 -1
u = 0 3 -2 u1 = 0 1 -2
0 0 8 0 0 1
Example with offset#
Below we will set the second input, k equal to 2. This will set the main diagonal and the diagonal immediately above to be equal to zero.
x = { 16 37 48 46
19 39 58 85
84 42 44 63
8 11 73 50 };
// Set the first 2 bands of the
// upper triangle to zero as well
// as the lower triangle
u = upmat(x, 2);
u = 0 0 48 46
0 0 0 85
0 0 0 0
0 0 0 0
Example with a vector return#
Below we will return only the upper triangular elements as a column vector.
x = { 16 37 48 46
19 39 58 85
84 42 44 63
8 11 73 50 };
u = upmat(x, 0, 1);
u = 16
37
48
46
19
39
58
85
84
42
44
63
8
11
73
50
Source#
diag.src