Working with Matrices
Create, index, slice, and manipulate arrays
Matrices are the foundation of numerical computing in Equana. This tutorial covers how to create arrays, access elements with indexing and slicing, and perform common matrix operations.
We'll work through examples interactively, with each code cell building on the previous. Run cells in sequence to see how variables persist across the notebook.
Creating Matrices
Create matrices using literal syntax with square brackets. Use commas (or spaces) to separate columns and semicolons to separate rows:
# Row vector
row = [1, 2, 3, 4, 5]
println(row)
# Column vector (semicolons create new rows)
col = [1; 2; 3; 4; 5]
println(col)
# 2D matrix (commas separate columns, semicolons separate rows)
A = [1, 2, 3; 4, 5, 6; 7, 8, 9]
println(A)# Using the range operator
x = 1:5
println(x)
y = 0:0.5:2
println(y)Creation Functions
Equana provides built-in functions for common matrix patterns:
| Function | Description | Example |
|---|---|---|
zeros(m, n) | Matrix of zeros | zeros(2, 3) |
ones(m, n) | Matrix of ones | ones(2, 3) |
linspace(a, b, n) | Linearly spaced vector | linspace(0, 1, 5) |
Z = zeros(2, 3)
println(Z)
O = ones(2, 3)
println(O)
# Linearly spaced values
x = linspace(0, 10, 5)
println(x)Indexing
Indexing is 1-based — the first element is at index 1. Use square brackets [] to access elements:
A = [10, 20, 30; 40, 50, 60; 70, 80, 90]
# Single element (row, column)
println(A[1, 1])
println(A[2, 3])
# Linear indexing
println(A[1])
println(A[5])
# Entire row or column
println(A[2, :])
println(A[:, 1])Note: The colon
:means "all elements" along that dimension.A[:, 2]gets all rows of column 2.
Slicing
Extract submatrices using range expressions with start:end or start:step:end syntax:
A = [1, 2, 3, 4, 5; 6, 7, 8, 9, 10; 11, 12, 13, 14, 15]
# First two rows
println(A[1:2, :])
# Columns 2 through 4
println(A[:, 2:4])
# Submatrix: rows 1-2, cols 2-4
println(A[1:2, 2:4])# Step slicing — every other element
x = [1, 2, 3, 4, 5]
println(x[1:2:5])
# Reverse with step
println(x[5:-1:1])Matrix Operators
Arithmetic operators work on matrices:
| Operation | Syntax |
|---|---|
| Addition | A + B |
| Subtraction | A - B |
| Multiplication | A * B |
| Division | A / B |
| Power | A ^ n |
A = [1, 2; 3, 4]
B = [5, 6; 7, 8]
# Addition
println(A + B)
# Multiplication
println(A * B)
# Power
println(A ^ 2)Comparison Operators
Comparisons work element-wise:
A = [1, 5, 3; 8, 2, 7]
println(A > 4)
println(A == 5)
println(A != 3)
# Combine with logical operators
println((A > 2) && (A < 6))
println((A < 2) || (A > 6))Matrix Information
Get information about matrix dimensions:
| Function | Description | Example |
|---|---|---|
size(A) | Dimensions (rows, cols) | size(A) |
size(A, dim) | Size along dimension | size(A, 1) |
length(A) | Total elements | length(A) |
ndims(A) | Number of dimensions | ndims(A) |
A = [1, 2, 3, 4; 5, 6, 7, 8; 9, 10, 11, 12]
println(size(A))
println(size(A, 1))
println(size(A, 2))
println(length(A))
println(ndims(A))Matrix Manipulation
Reshape
Change the shape of a matrix without changing its data:
A = [1, 2, 3, 4, 5, 6]
# Reshape to 2x3
B = reshape(A, 2, 3)
println(B)
# Reshape to 3x2
C = reshape(A, 3, 2)
println(C)Reverse
x = [1, 2, 3, 4, 5]
println(reverse(x))Aggregation Functions
Compute statistics across entire arrays or along specific dimensions:
| Function | Description |
|---|---|
sum(A, dim?) | Sum of elements |
mean(A, dim?) | Average |
min(A, dim?) | Minimum value |
max(A, dim?) | Maximum value |
std(A, dim?) | Standard deviation |
var(A, dim?) | Variance |
When dim is specified: dim=1 operates along columns, dim=2 operates along rows.
A = [1, 2, 3; 4, 5, 6; 7, 8, 9]
# Aggregate all elements
println(sum(A))
println(mean(A))
println(max(A))
# Aggregate along dimension
println(sum(A, 1))
println(sum(A, 2))
println(mean(A, 1))Linear Algebra
Equana includes essential linear algebra operations:
# Solve linear system Ax = b
A = [3, 1; 1, 2]
b = [9; 8]
x = solve(A, b)
println(x)
# Matrix inverse
println(inv(A))
# Determinant
println(det(A))
# Trace
println(trace(A))# Dot product
a = [1, 2, 3]
b = [4, 5, 6]
println(dot(a, b))
# Vector norm
println(norm(a))
# Eigenvalues
A = [3, 1; 1, 2]
println(eig(A))More functions: Check out the Linear Algebra package for decompositions (SVD, QR, Cholesky, LU) and more.
Extended Examples
Data Analysis
Analyze tabular data with matrix operations:
# Sample data: test scores for 5 students, 3 tests
scores = [85, 90, 78; 92, 88, 95; 76, 82, 80; 88, 91, 87; 95, 89, 92]
# Statistics per test (columns)
println(mean(scores, 1))
println(max(scores, 1))
# Statistics per student (rows)
println(mean(scores, 2))Matrix Arithmetic
Common matrix arithmetic patterns:
A = [1, 2; 3, 4]
B = [5, 6; 7, 8]
println(A + B)
println(A * B)
println(det(A))
println(trace(A))Summary
This tutorial covered the essentials of working with matrices in Equana:
- Creating matrices with literals, range operators, and creation functions
- Indexing with 1-based
[]bracket syntax - Slicing with range expressions and step sizes
- Operators for arithmetic and comparison
- Information functions like
size,length, andndims - Manipulation with
reshapeandreverse - Aggregation with
sum,mean,min,max, etc. - Linear algebra including
solve,inv,det,eig, and more
Next Steps
- Explore the Linear Algebra package for advanced decompositions and solvers
- Learn about string manipulation in the Working with Strings tutorial
- Try the FEM tutorial to see matrices used in finite element analysis