logo for matrixlab-examples.com
leftimage for matrixlab-examples.com

Matrices

The terms matrices and arrays are often used interchangeably. A matrix is a two-dimensional array of real or complex numbers that represent something.




The algebraic operations defined on matrices have applications in a broad variety of technical fields. Matlab has dozens of functions that create different kinds of matrices.
 
Arrays
of numbers can also compose matrices. To create a matrix, spaces or commas separate the elements in columns, semicolons separate rows.

>> g  =  [1  2  3  4;  5  6  7  8]

g  =

1    2    3    4
5    6    7    8

In this case g is a matrix of 2 rows and 4 columns (2x4).
Naturally, all of the rows must have the same number of columns.

Math with matrices and scalars
To an array or matrix, arithmetic with scalars equal to perform the operation of every element with the scalar.

>>  g+3           

ans  =           
4    5    6    7
8    9    10    11

>>  2*g-2           

ans  =           
0    2    4    6
8    10    12    14



Math between matrices
When two arrays have the same dimensions, you can add or subtract one to/from the other element by element, like this:

>>  A  =  [1  2  3  4;  5  6  7  8;  9  10  11  12]

A  =

1     2     3     4
5     6     7     8
9    10    11    12

>>  B  =  [1  1  1  1;  2  2  2  2;  2  2  2  2]

B  =

1    1    1    1
2    2    2    2
2    2    2    2

>>  A  +  B           

ans  =           
2      3     4      5
7      8     9      10
11    12    13    14

>> 2*A  -  B

ans  =
1      3     5     7
8      10   12    14
16    18    20    22

For multiplication, division or power operations, you use the 'element-wise' operators (with '.*', './', or '.^').

>>  A.*B

ans  =               
1      2     3      4   
10    12    14    16   
18    20    22    24   

>>  A./B               

ans  =               
1.0000    2.0000    3.0000    4.0000
2.5000    3.0000    3.5000    4.0000
4.5000    5.0000    5.5000    6.0000

>>  A.^2   
           
ans  =               
1      4       9       16   
25     36     49     64   
81    100    121    144   



Matrix Addressing
This notation refers to the element of row r and column c within matrix A.
A(r, c)   

This notation refers to all of the elements of row r in matrix A.
A(r, :)       

This notation refers to all of the elements of column c in matrix A.
A(:, c)       



Matrix Functions 
det(A) determinant of the square matrix A
inv(A) inverse of the square matrix A
norm(A) largest singular value of A
rank(A) provides an estimate of the number of linearly independent rows or columns of a matrix A
trace(A) sum of the diagonal elements of A, which is also the sum of the eigenvalues of A

Special Matrices
eye(N) is the N-by-N identity matrix
magic(N) an N-by-N matrix constructed from the integers 1 through N^2
ones(N) an N-by-N matrix of ones
rand(N) an N-by-N matrix containing pseudo-random values drawn from a uniform distribution on the unit interval.
zeros(N) an N-by-N matrix of zeros


From 'Matrices' to home

From 'Matrices' to 'Matlab Help Menu'
 



footer for matrices page