Docs/Core Functions

Core Functions

Essential functions for array creation, mathematics, aggregation, and manipulation. Always available.

zeros
Create array of all zeros
Z = zeros(n)
ones
Create array of all ones
X = ones(n)
eye
Identity matrix
I = eye(n)
magic
Magic square
M = magic(n)
linspace
Generate linearly spaced vector
v = linspace(a, b, n)
logspace
Generate logarithmically spaced vector
v = logspace(a, b)
rand
Uniformly distributed random numbers
R = rand(n)
randn
Normally distributed random numbers
R = randn(n)
meshgrid
Create 2D coordinate matrices
[X, Y] = meshgrid(x, y)
peaks
Peaks function (test surface)
Z = peaks()
gallery
Test matrices by name
A = gallery(matname)
sin
Sine of argument in radians
Y = sin(X)
cos
Cosine of argument in radians
Y = cos(X)
tan
Tangent of argument in radians
Y = tan(X)
asin
Inverse sine (arcsine)
Y = asin(X)
acos
Inverse cosine (arccosine)
Y = acos(X)
atan
Inverse tangent (arctangent)
Y = atan(X)
atan2
Four-quadrant inverse tangent
Y = atan2(B, A)
sinh
Hyperbolic sine
Y = sinh(X)
cosh
Hyperbolic cosine
Y = cosh(X)
tanh
Hyperbolic tangent
Y = tanh(X)
exp
Exponential (e^x)
Y = exp(X)
log
Natural logarithm
Y = log(X)
log10
Common logarithm (base 10)
Y = log10(X)
log2
Base-2 logarithm
Y = log2(X)
sqrt
Square root
Y = sqrt(X)
abs
Absolute value
Y = abs(X)
floor
Round toward negative infinity
Y = floor(X)
ceil
Round toward positive infinity
Y = ceil(X)
round
Round to nearest integer
Y = round(X)
sum
Sum of array elements
S = sum(A)
mean
Average of array elements
M = mean(A)
std
Standard deviation
S = std(A)
var
Variance
V = var(A)
min
Minimum value
M = min(A)
max
Maximum value
M = max(A)
any
Test if any elements are nonzero
B = any(A)
all
Test if all elements are nonzero
B = all(A)
find
Find indices of nonzero elements
k = find(X)
size
Array dimensions
s = size(A)
length
Length of largest array dimension
n = length(A)
numel
Number of array elements
n = numel(A)
ndims
Number of array dimensions
n = ndims(A)
reshape
Reshape array
B = reshape(A, m, n)
transpose
Transpose matrix
B = transpose(A)
diag
Create diagonal matrix or extract diagonal
D = diag(v)
flip
Flip order of elements
B = flip(A)
fliplr
Flip array left to right
B = fliplr(A)
flipud
Flip array up to down
B = flipud(A)
rot90
Rotate array 90 degrees
B = rot90(A)
lower
Convert string to lowercase
s = lower(str)
upper
Convert string to uppercase
s = upper(str)
strlength
Length of string
n = strlength(str)
strcmp
Compare strings
tf = strcmp(s1, s2)
strcmpi
Compare strings (case-insensitive)
tf = strcmpi(s1, s2)
strfind
Find pattern in string
k = strfind(str, pattern)
strrep
Replace substring
s = strrep(str, old, new)
strip
Remove leading and trailing whitespace
s = strip(str)
startsWith
Check if string starts with prefix
tf = startsWith(str, prefix)
endsWith
Check if string ends with suffix
tf = endsWith(str, suffix)
contains
Check if string contains pattern
tf = contains(str, pattern)
sprintf
Format data into string
s = sprintf(format, A1, A2, ...)
char
Convert to character
c = char(n)
num2str
Convert number to string
s = num2str(A)
str2num
Convert string to number
n = str2num(str)
strcat
Concatenate strings
s = strcat(s1, s2, ...)
strjoin
Join strings with delimiter
s = strjoin(strs, delimiter)
strsplit
Split string at delimiter
parts = strsplit(str, delimiter)
disp
Display value
disp(X)
tic
Start stopwatch timer
tic
toc
Read elapsed time from stopwatch
t = toc
eval
Evaluate M-Code expression
eval(expression)