Creating Matrices and Vectors
MATLAB (MATrix LABoratory) treats every piece of data as a matrix — a scalar is simply a 1x1 matrix, a vector is an Nx1 or 1xN matrix, and a full matrix is an MxN array. You create matrices literally with square brackets: elements in the same row are separated by spaces or commas, and rows are separated by semicolons. For example, A = [1 2 3; 4 5 6] builds a 2x3 matrix directly from its layout on the page.
Cricket analogy: A batting order is like a row vector — eleven names listed left to right — while a Test match scorecard across five days stacks those rows into a full matrix, just as MATLAB stacks rows with semicolons to build A = [1 2 3; 4 5 6].
Row and Column Vectors
Vectors come in two orientations that matter for arithmetic: a row vector like v = [1 2 3] is 1x3, while a column vector requires semicolons, v = [1; 2; 3], making it 3x1. The transpose operator ' flips one into the other, so v' converts a row into a column and vice versa; this distinction determines whether two vectors can be added, multiplied, or concatenated without a dimension-mismatch error.
Cricket analogy: A single over's six deliveries listed left to right is a row vector, while a bowler's five-over spell tallied one figure per line down the scorecard is a column vector, and MATLAB's transpose operator ' switches between the two views instantly.
Built-in Construction Functions
Beyond typing literal brackets, MATLAB provides functions to construct common matrices instantly: zeros(m,n) and ones(m,n) fill an array with 0s or 1s, eye(n) produces an n x n identity matrix, rand(m,n) fills with uniform random values, and the colon operator start:step:stop generates evenly spaced vectors such as 1:2:9, which yields [1 3 5 7 9]. linspace(a,b,n) instead fixes the number of points rather than the step size.
Cricket analogy: Setting a fielding template with everyone at zero runs before a match starts is like zeros(11,1), while eye(n) is like a perfectly balanced field where each fielder covers exactly one unique zone, no overlaps, no gaps.
% Row and column vectors
row = [1 2 3]; % 1x3
col = [1; 2; 3]; % 3x1
colFromRow = row'; % transpose row -> column
% Matrix literal
A = [1 2 3; 4 5 6]; % 2x3 matrix
% Construction functions
Z = zeros(2,3);
I = eye(3);
R = rand(2,2);
v = 1:2:9; % [1 3 5 7 9]
w = linspace(0,1,5); % 5 points from 0 to 1
disp(size(A)); % [2 3]Use size(A) to check dimensions before combining matrices — size returns [rows, cols], and numel(A) gives the total element count, both essential for debugging shape mismatches.
Concatenating rows or columns of mismatched sizes, e.g. [ [1 2]; [1 2 3] ], throws a dimension mismatch error at runtime, not at parse time — always verify row lengths match before using semicolons, and column heights match before using commas/spaces to horizontally concatenate.
- Every MATLAB value is a matrix; scalars are 1x1, vectors are Nx1 or 1xN.
- Square brackets build matrices literally; spaces/commas separate columns, semicolons separate rows.
- The transpose operator ' converts a row vector into a column vector and vice versa.
- zeros, ones, eye, and rand construct common matrices without typing every element.
- The colon operator start:step:stop generates evenly spaced vectors; linspace fixes the point count instead.
- size(A) and numel(A) reveal a matrix's dimensions and total element count.
- Mismatched row/column dimensions during concatenation raise a runtime dimension error.
Practice what you learned
1. Which MATLAB expression creates a 2x3 matrix?
2. What does eye(4) return?
3. How do you convert row vector v into a column vector?
4. What is the result of 1:2:9 in MATLAB?
5. Which function fixes the NUMBER of evenly spaced points rather than the step between them?
Was this page helpful?
You May Also Like
Indexing and Slicing in MATLAB
Master subscript and linear indexing, colon-based slicing, the end keyword, and logical indexing to access and filter matrix data efficiently.
Matrix Operations and Linear Algebra
Understand the distinction between matrix and element-wise operators, solve linear systems with backslash, and compute determinants, inverses, and eigenvalues.
Vectorization and Broadcasting
Learn to replace explicit loops with whole-array operations and use MATLAB's implicit expansion (broadcasting) rules for faster, more concise code.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics