Creates a diagonal matrix, or extracts a diagonal from a matrix.
Syntax |
A = Diag(B,k) |
|
Arguments |
Name |
Description |
B |
A vector or a matrix. |
|
k (optional) |
An index indicating the diagonal of interest. k = 0 (default) indicates the main diagonal. k is negative for sub-diagonals and positive for super diagonals. |
|
Outputs |
Name |
Description |
A |
A vector or a square matrix. When B is a vector, it will be placed on the diagonal of square matrix A indicated by k. When B is a matrix, the diagonal indicated by k will be extracted as vector A. |
|
Example 1 |
Create a 3 by 3 matrix with 1,2,3 on the main diagonal. |
|
Syntax |
|
|
A = Diag([1,2,3]) |
||
Results |
|
|
1 0 0 0 2 0 0 0 3 |
||
Example 2 |
Extract the first super diagonal from a matrix. |
|
Syntax |
||
A = Diag([1,2,3;4,5,6],1) |
||
Results |
||
2 6 |
||
See Also: |
Creates a matrix identity.
Syntax |
r = Eye() r = Eye(m) r = Eye(m, n) r = Eye(Size(A)) |
|
Arguments |
Name |
Description |
m |
A scalar. |
|
n |
A scalar. |
|
A |
A matrix. |
|
Outputs |
Name |
Description |
r |
An identity matrix. |
|
Example |
Syntax |
|
A = Eye(3, 5); print('A = ', A); |
||
Comments |
r = Eye(n) returns an n-by-n identity matrix. r = Eye(m, n) or r = Eye([m n]) returns an m-by-n identity matrix. r = Eye(Size(A)) returns an identity matrix with the same size as . r = Eye() with no arguments is the scalar 1. r = Eye() is equivalent to Eye(1, 1). |
|
The size inputs m or n should be non-negative integers. Negative integers are treated as 0. |
||
See Also: |
Linearly spaced vectors.
Syntax |
y = linspace(x1, x2) y = linspace(x1, x2,n) |
|
Arguments |
Name |
Description |
x1 |
Real or complex scalars. |
|
x2 |
Real or complex scalars. |
|
n |
Integer: the number of requested values (default value = 100). |
|
Outputs |
Name |
Description |
y |
Real or complex row vector. |
|
Example |
Syntax |
|
A = linspace(1, 50); print('A = ', A); A2 = linspace(1, 6, 2); print('A2 = ', A2); A3 = linspace(1, 4, 10); print('A3 = ', A3); A4 = linspace(1 + i, 2 + 2 * i,10); print('A4 = ', A4); |
||
Comments |
linspace(x1, x2) computes spaced samples over the interval [x1, x2]. |
|
See Also: |
Logarithmically spaced vector.
Syntax |
y = logspace(x1, x2) y = logspace(x1, x2, n) |
|
Arguments |
Name |
Description |
x1 |
Real or complex scalars. |
|
x2 |
Real or complex scalars. |
|
n |
An integer; the number of requested values (default value = 50). |
|
Outputs |
Name |
Description |
y |
A real or complex row vector. |
|
Example |
Syntax |
|
A = logspace(1, 2, 10); print(A) |
||
Comments |
logspace(x1, x2) computes spaced samples on a log scale. In this case, where x2 is equal to PI, the values of the points computed are between 10x1 and the PI value. |
|
See Also: |
Creates a matrix made of ones.
Syntax |
r = Ones() r = Ones(m) r = Ones(m, n) r = Ones(Size(A)) |
|
Arguments |
Name |
Description |
m |
A scalar. |
|
n |
A scalar. |
|
A |
A matrix. |
|
Outputs |
Name |
Description |
r |
A matrix of ones. |
|
Example |
Syntax |
|
A = Ones(3, 5); print('A = ', A); |
||
Comments |
r = Ones(n) returns an n-by-n matrix of ones. r = Ones(m, n) or r = Ones([m n]) returns an m-by-n matrix of ones. r = Ones(Size(A)) returns an array of ones that is the same size as A. r = Ones() with no arguments is the scalar 1. r = Ones() is equivalent to Ones(1, 1). The size inputs m or n should be non-negative integers. Negative integers are treated as 0. For large matrices, HML scripts may execute faster if the ones function is used to set aside storage for a matrix whose elements are to be generated one at a time, or a row or column at a time. For example: N = 1e5; t1 = CpuTime(); x = Ones(1,N); t2 = CpuTime(); t3 = CpuTime(); x = []; for k = 1,N do x(k) = 1; end t4 = CpuTime(); print('Ones: t2 - t1 = ', t2 - t1) print('loop: t4 - t3 = ', t4 - t3) If you need to create a matrix whose values are all the same, you should use an expression like: A = ones(m, n); B = scalarvalue * A; |
|
See Also: |
Change the dimensions of a matrix.
Syntax |
B = Reshape(A,m,n) B = Reshape(A,[m,n]) |
|
Arguments |
Name |
Description |
|
A |
A matrix to be reshaped. |
|
m |
The number of rows of the new matrix. |
|
n |
The number of columns of the new matrix. |
Outputs |
Name |
Description |
|
B |
The output matrix. It must have the same size as A. |
Example 1 |
Change the shape of [1,3,5;2,4,6]. |
|
|
Syntax |
|
|
B = Reshape([1,3,5;2,4,6], 3, 2) |
|
|
Results |
|
|
B = 1 4 2 5 3 6 |
|
Comments |
A must contain m*n elements. |
Creates a lower triangular matrix from an input matrix.
Syntax |
A = Tril(B,k) |
|
Arguments |
Name |
Description |
B |
A matrix. |
|
k (optional) |
An index indicating the uppermost diagonal of the input matrix to be preserved in the output matrix. k = 0 (default) indicates main diagonal. k is negative for sub diagonals and positive for super diagonals. |
|
Outputs |
Name |
Description |
A |
A lower triangular matrix. |
|
Example |
Create a lower triangular matrix from a 3 by 3 matrix. |
|
Syntax |
|
|
A = Tril([1,2,3;4,5,6;7,8,9]) |
||
Results |
|
|
A = [1,0,0 4,5,0 7,8,9] |
||
See Also: |
Creates an upper triangular matrix from an input matrix.
Syntax |
A = Triu(B,k) |
|
Arguments |
Name |
Description |
B |
A matrix. |
|
k (optional) |
An index indicating the lowest diagonal of the input matrix to be preserved in the output matrix. k = 0 (default) indicates main diagonal. k is negative for sub diagonals and positive for super diagonals. |
|
Outputs |
Name |
Description |
A |
An upper triangular matrix. |
|
Example |
Create an upper triangular matrix from a 3 by 3 matrix. |
|
Syntax |
|
|
A = Triu([1,2,3;4,5,6;7,8,9]) |
||
Results |
|
|
A = [1,2,3 0,5,6 0,0,9] |
||
See Also: |
Creates a matrix made of zeros.
Syntax |
r = Zeros() r = Zeros(m) r = Zeros(m, n) r = Zeros(Size(A)) |
|
Arguments |
Name |
Description |
m |
A scalar. |
|
n |
A scalar. |
|
A |
A matrix. |
|
Outputs |
Name |
Description |
r |
A matrix of zeros. |
|
Example |
Syntax |
|
A = Zeros(3, 5); print('A = ', A); |
||
Comments |
r = Zeros(n) returns an n-by-n matrix of zeros. r = Zeros(m, n) or r = Zeros([m n]) returns an m-by-n matrix of zeros. r = Zeros(Size(A)) returns an array of zeros that is the same size as A. r = Zeros() with no arguments is the scalar 0. r = Zeros() is equivalent to Zeros(1, 1). The size inputs m or n should be nonnegative integers. Negative integers are treated as 0. For large matrices, HML scripts may execute faster if the zeros function is used to set aside storage for a matrix whose elements are to be generated one at a time, or a row or column at a time. For example: N = 1e5; t1 = CpuTime(); x = Zeros(1,N); t2 = CpuTime(); t3 = CpuTime(); x = []; for k = 1,N do x(k) = 0; end t4 = CpuTime(); print('Zeros: t2 - t1 = ', t2 - t1) print('loop: t4 - t3 = ', t4 - t3) |
|
See Also: |