Built-in Mathematical and Statistical Functions
MATLAB's built-in functions work on whole arrays.
Concept
MATLAB's built-in functions work on whole arrays. Most operate column by column on matrices by default; pass a dimension argument to change that (sum(A, 2) sums each row).
sqrt, abs, sign, exp, log, log10, log2, power, hypot
sin cos tan (radians), sind cosd tand (degrees), asin acos atan atan2, deg2rad, rad2deg
round, floor, ceil, fix, mod, rem sum, prod, cumsum, cumprod, mean, median, mode, std, var, min, max, range
sort, sortrows, unique, find, ismember, any, all diff, gradient
factorial, nchoosek, primes, isprime, gcd, lcm Many return extra outputs: [m, i] = max(v) returns the value and its index; [s, idx] = sort(v) returns the sorted values and the permutation.
Why It Matters
Built-in functions are fast (compiled and optimized), tested and vectorized; using them instead of hand-written loops makes code shorter, faster and more reliable.
Syntax
[m, idx] = max(data); % value and position
s = sum(A, 2); % row sums
[vals, order] = sort(v, 'descend');Example
7.1 Everyday numerical functions
data = [12.4 12.6 11.9 12.5 13.1 12.2];
fprintf('sum %.1f, mean %.3f, median %.2f, std %.4f, var %.4f\n', sum(data), mean(data), median(data), std(data), var(data))
[mx, imx] = max(data);
[mn, imn] = min(data);
fprintf('max %.1f at index %d, min %.1f at index %d, range %.1f\n', mx, imx, mn, imn, mx - mn)
[s, idx] = sort(data, 'descend');
fprintf('sorted descending: %s (original positions %s)\n', mat2str(s), mat2str(idx))
fprintf('cumsum of 1:5 = %s, prod = %d, diff of [1 4 9 16] = %s\n', mat2str(cumsum(1:5)), prod(1:5), mat2str(diff([1 4 9 16])))
fprintf('sqrt(16) = %g, exp(1) = %.5f, log(100) = %.4f, log10(100) = %g, log2(8) = %g\n', sqrt(16), exp(1), log(100), log10(100), log2(8))
fprintf('sin(pi/6) = %.4f, cosd(60) = %.4f, atan2(1, 1) = %.4f rad\n', sin(pi/6), cosd(60), atan2(1, 1))
fprintf('abs(-3) = %g, sign(-3) = %g, factorial(5) = %g, nchoosek(5, 2) = %g, gcd(12, 18) = %g, lcm(4, 6) = %g\n', ...
abs(-3), sign(-3), factorial(5), nchoosek(5, 2), gcd(12, 18), lcm(4, 6))
fprintf('isprime(97) = %d, primes(20) = %s\n', isprime(97), mat2str(primes(20)))
M = [1 2; 3 4];
fprintf('sum(M) = %s (per column), sum(M, 2) = %s (per row), sum(M(:)) = %d\n', mat2str(sum(M)), mat2str(s
…Expected Result
sum(M) summed each column (4 6), sum(M, 2) each row, and sum(M(:)) everything. sort also returned where each value came from.
sum 74.7, mean 12.450, median 12.45, std 0.4037, var 0.1630
max 13.1 at index 5, min 11.9 at index 3, range 1.2
sorted descending: [13.1 12.6 12.5 12.4 12.2 11.9] (original positions [5 2 4 1 6 3])
cumsum of 1:5 = [1 3 6 10 15], prod = 120, diff of [1 4 9 16] = [3 5 7]
sqrt(16) = 4, exp(1) = 2.71828, log(100) = 4.6052, log10(100) = 2, log2(8) = 3
sin(pi/6) = 0.5000, cosd(60) = 0.5000, atan2(1, 1) = 0.7854 rad
abs(-3) = 3, sign(-3) = -1, factorial(5) = 120, nchoosek(5, 2) = 10, gcd(12, 18) = 6, lcm(4, 6) = 12
isprime(97) = 1, primes(20) = [2 3 5 7 11 13 17 19]
sum(M) = [4 6] (per column), sum(M, 2) = [3 7] (per row), sum(M(:)) = 10
unique([3 1 3 2 1]) = [1 2 3], numel(find([0 1 0 1 1])) = 3Common Mistakes
Expecting sum(A) of a matrix to be a scalar.
Using degrees with sin (use sind or convert with deg2rad).
std normalizes by N−1 (sample standard deviation) by default; use std(x, 1) for N.
Shadowing built-ins with variables named sum, max, min, mean.
Best Practices
Look for a built-in before writing a loop (help + "See also" links).
Use the second output of max/min/sort instead of searching again with find.
Specify the dimension explicitly for matrices to make intent clear.
Interview Relevance
Medium: "how do you find the index of the maximum?", "column-wise vs row-wise operations?", "find vs logical indexing?".
Practice Questions
Find the two largest values in a vector and their positions.
Compute the percentage change between consecutive samples with diff.
Count how many values in a vector are greater than the mean.
Find the most frequent value with mode.
Quick Quiz
Q1. (Multiple choice) sum(A) for a 3×4 matrix returns: A) A scalar B) A 1×4 row of column sums C) A 3×1 column of row sums D) An error Answer: B.
Q2. (Predict the output) diff([1 4 9 16]) Answer: [3 5 7] (verified).
Q3. (True/False) sin(90) returns 1.
Answer: False. sin uses radians; sind(90) returns 1.
Q4. (Find the bug) After max = 10; the call max([1 5 3]) fails.
Answer: The variable max shadows the function; clear max and rename the variable.
Coding Challenge
For the daily battery temperatures [21 23 22 25 30 29 24], print the hottest day (1-based index), the average, the days above average, and the largest day-to-day increase.
temps = [21 23 22 25 30 29 24];
[hot, day] = max(temps);
avg = mean(temps);
fprintf('hottest: day %d (%d C)\n', day, hot)
fprintf('average: %.2f C, days above average: %s\n', avg, mat2str(find(temps > avg)))
[jump, at] = max(diff(temps));
fprintf('largest increase: %d C from day %d to day %d\n', jump, at, at + 1)