Matrix Creation

The [ and ] operators are used to create and access a matrix. For example, to create a matrix at the command line, you would just type:
> m = [ 1, 2, 3; 4, 5, 6; 7, 8, 9 ]
 m =
 matrix columns 1 thru 3
        1          2          3  
        4          5          6  
        7          8          9
The rows of the matrix are delimited with `;' and the elements of each row are delimited with `,'. If you want a complex matrix you enter it using the normal notation of real and imaginary parts. Here is an example:
> z= [ 1+2i, 2+3j, 3 + 4i;
>      4 -5i, -5+6j, -6 -7j ]
 z =
 matrix columns 1 thru 3
                  1 + 2i                  2 + 3i                  3 + 4i
                  4 - 5i                 -5 + 6i                 -6 - 7i
This example shows a few things that are pretty important. You can use either i or j, but you can't leave spaces between the imaginary part and the letter. Spaces are generally fine everywhere else. While the previous method for matrix creation is quite convenient, often you will need specific types of matrices, such as an identity matrix, a matrix of random values, or a Hilbert matrix. There are functions to produce these and many other types of matrices. All functions are described in Chapter [*], however those that are likely to be of particular interest for creating matrices are:
compan
generates the companion matrix of its argument
diag
diagonalises its argument
eye
produces an identity matrix
hilb
produces a Hilbert matrix
linspace and logspace
produce spaced vectors
ones
produce a matrix with all elements 1.
rand
generates random matrix
reshape
changes the structure of a matrix
zeros
produce a matrix with all elements 0.
Another useful method for creating a matrix is to read the values from a file. provides several ways of doing this. The two most popular are readm() and read(). read() reads a matrix that has been written with the write() function (See Section [*] and Section [*]). Here is a contrived example - you would normally write it out in one session, and read it again in a later session. You should also realise that read and write are not restricted to a storing and retrieving a single variable, or even a single type of variable, in a file. You can store several different types of variables in each file, according to your needs.
> z = [ 3 , 4; -4-3j , 786 ] 
 z =
                  3 + 0i                  4 + 0i
                 -4 - 3i                786 + 0i
> who()
eps  pi   z              
> write( "save_z.dat" , z )
        1
> // We have now written out the matrix `z' to the file `save_z.dat'
> clear(z);
> who()
eps  pi                  
> // The matrix `z' is now gone from the symbol table
> read( "save_z.dat" )
        1
> who()
eps  pi   z              
> // And now it has been read back in.
> diary()
readm() (See Section [*]) reads a text file that contains white-space separated columns of numbers. This is useful if you are trying to use the output of other programs. If you want to, you can also write out a matrix in the same format, using the writem function. If you don't like the way the matrix is organised, the reshape() function (See Section [*]) is a good way to restructure it.
> 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  
> // Now we write the matrix to a file.
> writem("matrix_text", a)
        1
> clear(a)
        1
> who()
eps  pi                  
> // And get it back - store it in a different variable this time
> b = readm("matrix_text")
 b =
        1          2          3          4  
        5          6          7          8  
        9         10         11         12  
> // But we want those elements organised as two rows and six columns
> c = reshape(b, 2,6)
 c =
 matrix columns 1 thru 6
        1          9          6          3         11          8  
        5          2         10          7          4         12
It appears that things have gone horribly wrong in that last example. However this is expected behavior. The matrix is stored column by column, and if you run down each column in each matrix, the order is the same in each matrix. The transposition operator might come in handy if you want it to work by rows, as shown here:
> d = reshape(b', 2,6)
 d =
 matrix columns 1 thru 6
        1          3          5          7          9         11  
        2          4          6          8         10         12  
> e = (reshape(b',6,2))'
 e =
 matrix columns 1 thru 6
        1          2          3          4          5          6  
        7          8          9         10         11         12
Sometimes when you have a problem that is represented as two matrices, you would like to combine them to make a single matrix. This is a pretty simple task as soon as you realise that there is nothing that makes the things inside the square brackets have to be scalars —they can also be strings and other matricies. We will look at using matricies here, and deal with strings later. When trying to tack several matricies together to make a new matrix, or using a combination of scalars and matricies to make a new matrix, you should think of the matricies as being equivalent to how they would be represented if you had to type them yourself. So imagine that each row is terminated with a semicolon and each element seperated by a comma. Then fill in commas and semicolons between the elements to make up the new matrix. Here are some examples:
> a = [1, 2,3; 4,5,6]
 a =
        1          2          3  
        4          5          6  
> b = [ 7, 8, 9; 10, 11, 12]
 b =
        7          8          9  
       10         11         12  
> c = [a,b]
 c =
 matrix columns 1 thru 6
        1          2          3          7          8          9  
        4          5          6         10         11         12  
> d = [a;b]
 d =
        1          2          3  
        4          5          6  
        7          8          9  
       10         11         12  
> e = [ -2, -1 ,0; a]
 e =
       -2         -1          0  
        1          2          3  
        4          5          6