>> A = [1,4, 9; 2, 3, 5; -2, 5, 10] 1 4 9 2 3 5 -2 5 10 >>
Create Matrices
Alternatively, a matrix of a required size can be created and initialized using built-in functions zeros, ones, or rand. The command
A = zeros(3, 5)will return a matrix of three rows and five columns, with each element being zero. Similar usage of ones and rand will create matrices of 1's and random numbers (between 0 and 1) respectively.
These three functions can also be called with a single parameter, in which case the second parameter is assumed to be 1, and thus a column vector is created.
>> B = rand(5) 0.289 0.353 0.154 0.566 0.821 >>By the dimension of a matrix we refer to the number of rows and the number of columns. For example, the dimension of the scalar -5 is , while the dimension of
-2 3 9 10 1 -2is .
Create Even Spaced Vectors Using the Colon Operator
The symbol : can be used to create a row matrix whose elements are evenly spaced. The default step-size of the vector is 1, which is assumed when one colon is used.
>> A = -1 : 5 -1 0 1 2 3 4 5To specify a step-size other than 1, two colons are needed.
>> A = 3 : 0.5 : 5 3 3.5 4 4.5 5
Scalar, vector, and matrix
Every numerical value is treated as a matrix. It is only for convenience that sometimes we call some special matrices scalars or vectors. There is no distinction between a scalar, a, row or column vector of length 1, or matrix. Likewise, a row vector of five elements is the same as a matrix, and a column vector of five elements is the same as a matrix.
oz 2009-12-22