2012年9月15日 星期六

Scilab fundamentals 4 - Create a matrix

    Several variables were introduced in Scilab fundamentals 2, but it’s clarified that everything in Scilab is a matrix. Strictly speaking, all variables (real, complex, integer, string, polynomial, and Boolean variables) are matrices. Therefore, understanding the operations and calculations of matrices helps us program Scilab codes. The first problem we encounter is how to define a matrix. Let’s start from this point, and then extend to other matrix techniques.


    To deal with a matrix, we have to define the matrix dimension and data type. Fig. 1 shows the structure of a 2-D matrix. The numbers of row and column define the matrix size and the number of elements in this matrix. This matrix shown in Fig. 1 comprises m rows and n columns. The subscripts, i and j, are the index numbers for row and column, respectively. Consequently, the dimension of this 2-D matrix is m×n.
 
Fig. 1 A 2-D matrix
 
Example-1 Create a 5×3 real variable matrix as shown below.
 
 
    To create a matrix, all data are bounded by square brackets, [ and ]. The values in different rows are separated by semicolons, and those in different columns are separated by commas or space. Type the command below on command line to create this matrix,
 
A=[1 6 11; 2 7 12; 3 8 13; 4 9 14; 5 10 15]
 
and the result is shown in Fig. 2.
 
Fig. 2 A 5×3 real variable matrix
 
    A 2-D matrix often arises in numerical analysis and plays an important role in linear algebra, i.e. Gauss-Seidel elimination. High-dimension matrices are also available. For instance, a 3-D matrix may descript the temperature distribution of a cubic (3-D matrices will be introduced in other section). New users should pay attention to the fundamental matrix, the 1-D matrix, which composes single column or single row. In Scilab, a 1-D matrix is named vector. Fig. 3 and Fig. 4 show a single-column vector and a single-row vector, respectively.
 
Fig. 3 A single-column vector
 
Fig. 4 A single-row vector
 
    Scilab also provides several built-in functions for users to create special matrices. These functions are listed in Table 1, and Fig. 5 shows the examples of the corresponding matrices.
 
 

Fig. 5 The special matrices created by functions, (a) a 3×5 matrix made of zeros, (b) a 3×3 matrix made of ones, (c) a 5×5 identity matrix, (d) a 4×4 matrix with random elements, (e) a 3×3 magic matrix, (f) a row vector.

沒有留言:

張貼留言