 # Matrix manipulation

### Introduction and Goals

Matrices are the fundamental data type in GAUSS. This tutorial will show you how to

## 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

1.1    2.1
3.1    4.1
5.1    6.1

7
8
9```

Scalar values in GAUSS are 1x1 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.

Function Return
`rows` The number of rows of a matrix.
`cols` The number of columns of a matrix.
`getOrders` All dimensions of a matrix or multi-dimensional array.
``````// 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

Operator Purpose
`~` The tilde operator performs horizontal matrix concatenation.
`|` The pipe operator performs vertical matrix concatenation.

## 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:

Operator   Purpose
`:`   The colon operator specifies a range
`.` The dot operator indicates all elements
`0` A scalar zero indicates all elements. This is deprecated, use the dot operator instead

``````// 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'
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```

``````// 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.

### Have a Specific Question?

Get a real answer from a real person

### Need Support?

Get help from our friendly experts.