GEOSC 444
Matlab Application for Geoscience

Lesson 2: 2-D matrices, special matrices

PrintPrint

Syntax introduced:

ones zeros eye repmat

2-D matrix creation

A 2-D matrix is created inside of square brackets with spaces or commas between elements in a row and semicolons or returns separating rows. Rows have to have the same number of elements. You can use multiple linspace or : inside the square brackets, too.

Special matrices

Special matrices include zeros(numberOfRows, numberOfColumns), ones(numberOfRows, numberOfColumns), and eye(number). The identity matrix eye, in which ones appear along the diagonal but all other elements equal zero, is always square so you just specify one number for the dimension.

>> zeros(2,3)
ans =
     0     0     0
     0     0     0
>> ones(5,2)
ans =
     1     1
     1     1
     1     1
     1     1
     1     1
>> eye(6)
ans =
     1     0     0     0     0     0
     0     1     0     0     0     0
     0     0     1     0     0     0
     0     0     0     1     0     0
     0     0     0     0     1     0
     0     0     0     0     0     1