Lesson 21 of 55
10 mins readPython Matrices & Multidimensional Arrays
In Plain English
Julia has native support for N-dimensional arrays. Spaces separate columns in rows, semicolons separate rows, and data is stored in high-speed column-major order.
Deep Dive: How It Works
Matrix Syntax: [1 2 3; 4 5 6] defines a 2x3 Matrix.
Column-Major Order: Julia stores array elements in memory down columns first (like Fortran and MATLAB, unlike C/Python row-major). Iterating column-by-column is much faster.
Indexing: M[row, col] accesses a specific cell. M[:, 2] extracts column 2 as a vector. M[1, :] extracts row 1.
Generators: ones(3, 3), zeros(2, 4), rand(3, 3), reshape(1:6, 2, 3).
Core Rules to Remember

Native Matrix Literal Syntax: Space for horizontal concatenation (columns), semicolon for vertical concatenation (rows).

Column-Major Performance: Fastest loop order is outer over columns, inner over rows (for j in 1:cols, i in 1:rows).
Live Interactive Example
Hit Run Code to see it liveMatrix Construction and Slicing
Python 3.12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Output Console
Click "Run Code" to view the rendered output.
How it works: Reshape fills elements column-by-column due to column-major layout.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Build a 2x2 Matrix
Define a 2x2 matrix `M = [10 20; 30 40]`.
Print `"Cell [1,2]: $(M[1, 2])"`.
Print `"Cell [2,1]: $(M[2, 1])"`.
1
2
3
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.