100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Matrices and Vectors in MATLAB

Learn how MATLAB represents scalars, vectors, and matrices as a single unified array type, and how to construct them with literals and built-in functions.

Matrices & ArraysBeginner8 min readJul 10, 2026
Analogies

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.

matlab
% 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

Was this page helpful?

Topics covered

#Programming#MATLABStudyNotes#MatricesAndVectorsInMATLAB#Matrices#Vectors#MATLAB#Creating#StudyNotes#SkillVeris#ExamPrep