INTRODUCTION AND GOALS
Matrices are the fundamental data type in GAUSS. This tutorial will show you how to
- Create matrices
- Find the size of a matrix
- Concatenate matrices
- Index matrices
CREATING MATRICES
DECLARING MATRICES
GAUSS uses curly braces {}
to surround literal matrix declarations and commas to separate rows. For example:
// Create a 1x6 row vector a = { 1.1 2.2 3.3 4.4 5.5 6.6 }; // Create a 3x2 matrix // NOTE: The rows do not have to be on // separate lines, but this style // can make it more clear b = { 1.1 2.1, 3.1 4.1, 5.1 6.1 }; // Create a 3x1 column vector c = { 7, 8, 9 }; print a; print b; print c;
will return the following output
1.1 2.2 3.3 4.4 5.5 6.6 3.1 4.1 5.1 6.1 7 8 9
Scalar values in GAUSS are 1×1 matrices and can be declared with or without the curly braces, for example:
// Both statements below are equivalent d = { 3.14 }; d = 3.14;
CREATING MATRICES WITH FUNCTIONS
Many GAUSS functions will create matrices. Here are some helpful functions for creating common matrices
// Create a 4x3 matrix of ones e = ones(4,3); // Create a 6x2 matrix of zeros e = zeros(6,2); // Create a 100x4 matrix of // random normal numbers N ~ (0, 1) g = rndn(100, 4); // Create a column vector, containing // the sequence 1, 3, 5, 7, 9 // the 'a' in 'seqa' stands for add h = seqa(1, 2, 5); // Create a column vector, containing // the sequence 1, 2, 4, 8, 16 // the 'm' in 'seqm' stands for multiply i = seqm(1, 2, 5); // Create a 4x4 identity matrix j = eye(4);
GETTING THE SIZE OF MATRICES
You can find the size of a GAUSS matrix by using the functions in the table below.
// Create a 270x11 matrix with uniform // random numbers between 0 and 1 k = rndu(270, 11); print rows(k); print cols(k);
and
// Print the number of rows and columns in 'k' print getOrders(k);
will both return the following output:
270 11
CONCATENATION OPERATORS
EXAMPLES: MATRIX CONCATENATION
HORIZONTAL MATRIX CONCATENATION
// Create a 3x1 matrix of ones m = ones(3, 1); // Create a 3x2 matrix n = { 4 5, 6 7, 8 9 }; // Horizontally concatenate the column // vector 'm' and the matrix 'n' p = m ~ n; print p;
will return
1 4 5 1 6 7 1 8 9
VERTICAL MATRIX CONCATENATION
// Create a 2x2 identity matrix q = eye(2); // Create a 3x2 matrix r = { 4 5, 6 7, 8 9 }; // Vertically concatenate the // matrices 'q' and 'r', with the // pipe '|' operator s = q | r; print s;
will return
1 0 0 1 4 5 6 7 8 9
COMBINED VERTICAL AND HORIZONTAL CONCATENATION
Since the concatenation operators have the same operator precedence, you must separate compound statements with parentheses.
t = (1|2) ~ (3|4); print t;
will return
1 3 2 4
INDEXING OPERATORS
GAUSS uses square brackets []
for indexing matrices. The following operators are also used with indexing:
EXAMPLES: READING MATRIX ELEMENTS
READ A SINGLE ELEMENT
// Create a 3x2 matrix b = { 1.1 2.1, 3.1 4.1, 5.1 6.1 }; // Extract a single element from the // 3rd row and 1st column of 'b'
© 2024 Aptech Systems, Inc. All rights reserved.
b_idx = b[3,1]; print b_idx;
will return
5.1
READ A RANGE FROM COLUMN
// Create a 3x2 matrix b = { 1.1 2.1, 3.1 4.1, 5.1 6.1 }; // Extract the elements from the 2nd // and 3rd rows of the 2nd column of 'b', // using the colon ':' operator b_idx = b[2:3,2]; print b_idx;
will return
4.1 6.1
READ AN ENTIRE COLUMN
// Create a 3x2 matrix b = { 1.1 2.1, 3.1 4.1, 5.1 6.1 }; // Extract all elements from the 1st column of 'b', // using the dot '.' operator b_idx = b[.,1]; print b_idx;
will return
1.1 3.1 5.1
READ SPECIFIED ELEMENTS FROM A COLUMN
// Create a 3x2 matrix b = { 1.1 2.1, 3.1 4.1, 5.1 6.1 }; // Read the 1st and 3rd elements from the // 2nd column b_idx = b[1 3,2]; print b_idx;
will return
2.1 6.1
EXAMPLES: WRITING MATRIX ELEMENTS
WRITE A SINGLE ELEMENT
// Create a 3x2 matrix b = { 1.1 2.1, 3.1 4.1, 5.1 6.1 }; // Write over the element in the // 3rd row and 2nd column of 'b' b[3,2] = 99; print b;
will return
1.1 2.1 3.1 4.1 5.1 99
WRITE A RANGE FROM COLUMN
// Create a 3x2 matrix b = { 1.1 2.1, 3.1 4.1, 5.1 6.1 }; // Copy the 1st and 2nd elements of the 2nd column of 'b' // over the 2nd and 3rd elements of the 2nd column of 'b' b[2:3,2] = b[1:2,2]; print b;
will return
1.1 2.1 3.1 2.1 5.1 4.1
WRITE TO AN ENTIRE COLUMN
// Create a 3x2 matrix b = { 1.1 2.1, 3.1 4.1, 5.1 6.1 }; // Write over the entire 1st column of 'b', // using the dot '.' operator b[.,1] = 28 | 29 | 30; print b;
will return
28 2.1 29 4.1 30 6.1
WRITE AN ENTIRE ROW
// Create a 3x2 matrix b = { 1.1 2.1, 3.1 4.1, 5.1 6.1 }; // Write over all elements from the 1st row of 'b' // using the dot '.' operator b[1,.] = 0.5 ~ 0.9; print b;
will return
0.5 0.9 3.1 4.1 5.1 6.1
WRITE ELEMENTS TO SPECIFIC ROW AND COLUMN INDICES
// Create a 3x3 matrix b = { 8 6 3 5, 0 9 10 1, 2 7 4 7 }; // Create a 2x2 matrix c = { 1.1 3.3, 2.2 4.4 }; // Write a 2x2 matrix to specified // non-contiguous elements of 'b' b[1 3, 2 4] = c; print b;
will return
8 1.1 3 3.3 0 9 10 1 2 2.2 4 4.4
WRITING TO MATRIX ELEMENTS WHICH ARE OUT-OF-RANGE
GAUSS does not allow you to write to a location in a matrix which does not exist. For example:
// Create a 1x2 matrix m = { 125 171 }; // INCORRECT // Attempt to write to the 2nd row // of 'm' which does not exist m[2,.] = 98 ~ 131;
will cause GAUSS to return the error G0058: Index out of range
. The reason that GAUSS does not allow this, is to protect you from making an unintended resizing of the matrix. If you would like to add rows or columns to your GAUSS matrix, use the concatenation operators ~
, |
instead.
CONCLUSION
You have learned how to:
- Create matrices with a declaration and with some functions
- Find the dimensions of a matrix
- Concatenate matrices
- Read from and write to sections of a matrix by index