Differences between revisions 1 and 2
Revision 1 as of 2026-01-18 23:10:41
Size: 583
Comment: Initial commit
Revision 2 as of 2026-01-19 17:00:14
Size: 1690
Comment: Indexing notes
Deletions are marked like this. Additions are marked like this.
Line 32: Line 32:
=== Indexing ===

Matrices are indexed from 1, by row then column.

{{{
julia> A = [1 2 3; 4 5 6; 7 8 9]
3×3 Matrix{Int64}:
 1 2 3
 4 5 6
 7 8 9

julia> A[1,2]
2
}}}

To select entire rows or columns, use the range operator `:` like:

{{{
julia> A[3,:]
3-element Vector{Int64}:
 7
 8
 9

julia> A[:,3]
3-element Vector{Int64}:
 3
 6
 9
}}}

Naturally, it is possible to select a range that is not the entire span.

{{{
julia> A[2:3,2:3]
2×2 Matrix{Int64}:
 5 6
 8 9
}}}

As an example of how to apply these indexing operations, consider the below solution to a linear system using Cramer's rule.

{{{
using LinearAlgebra

# Note: replacing 1 with 1//1 causes automatic promotion to Rational, so that answers are exact and without rounding errors from floating point arithmetic
A = [1//1 1 -1; 3 -2 1; 1 3 -2]
b = [6; -5; 14]

d = det(A)

A1 = [b A[:,2:3]]
println("x = ",det(A1)/d)

A2 = [A[:,1] b A[:,3]]
println("y = ",det(A2)/d)

A3 = [A[:,1:2] b]
println("z = ",det(A3)/d)
}}}

This gives the expected solution ''[1 3 -2]''.


Julia Matrices

Matrices are a 2-dimensional shaped series of values.


Description

Matrices are instantiated using a syntax that is consistent with that of vectors.

  • Rows are entered as space-delimited values.
  • To indicate the separation between rows, use either a literal newline or the vertical concatenation operator (;).

julia> a = [1 2; 3 4]
2×2 Matrix{Int64}:
 1  2
 3  4

julia> a = [1 2
            3 4]
2×2 Matrix{Int64}:
 1  2
 3  4

Indexing

Matrices are indexed from 1, by row then column.

julia> A = [1 2 3; 4 5 6; 7 8 9]
3×3 Matrix{Int64}:
 1  2  3
 4  5  6
 7  8  9

julia> A[1,2]
2

To select entire rows or columns, use the range operator : like:

julia> A[3,:]
3-element Vector{Int64}:
 7
 8
 9

julia> A[:,3]
3-element Vector{Int64}:
 3
 6
 9

Naturally, it is possible to select a range that is not the entire span.

julia> A[2:3,2:3]
2×2 Matrix{Int64}:
 5  6
 8  9

As an example of how to apply these indexing operations, consider the below solution to a linear system using Cramer's rule.

using LinearAlgebra

# Note: replacing 1 with 1//1 causes automatic promotion to Rational, so that answers are exact and without rounding errors from floating point arithmetic
A = [1//1 1 -1; 3 -2 1; 1 3 -2]
b = [6; -5; 14]

d = det(A)

A1 = [b A[:,2:3]]
println("x = ",det(A1)/d)

A2 = [A[:,1] b A[:,3]]
println("y = ",det(A2)/d)

A3 = [A[:,1:2] b]
println("z = ",det(A3)/d)

This gives the expected solution [1 3 -2].


CategoryRicottone

Julia/Matrices (last edited 2026-01-19 17:00:14 by DominicRicottone)