Skip to content
Vectors and Matrices
step 1/5

Reading — step 1 of 5

Learn

~1 min readNumerical Basics

Vectors and matrices are the core data type. Octave makes constructing them direct:

v = [1, 2, 3, 4, 5]            % row vector
u = [1; 2; 3]                  % column vector (semicolons = row breaks)
M = [1, 2, 3; 4, 5, 6; 7, 8, 9]  % 3x3 matrix

v(1)        % 1 (1-indexed!)
v(2:4)      % [2 3 4] — slice
v(end)      % 5 — last element
length(v)   % 5

Vectorized arithmetic — operations apply element-wise:

v * 2       % [2 4 6 8 10]
v .^ 2      % [1 4 9 16 25] — note .^ for element-wise power
v .* w      % element-wise multiply (* is matrix multiply!)
sum(v)      % 15
mean(v)     % 3
max(v)      % 5

The . prefix means "element-wise" — .*, ./, .^. Plain * does matrix multiplication.

Range syntax generates sequences:

1:5        % [1 2 3 4 5]
1:2:10     % [1 3 5 7 9] — start:step:end

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…