# 8.1.1: Built-in MATLAB Functions of Vectors

$$\newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} }$$

$$\newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}}$$

$$\newcommand{\id}{\mathrm{id}}$$ $$\newcommand{\Span}{\mathrm{span}}$$

( \newcommand{\kernel}{\mathrm{null}\,}\) $$\newcommand{\range}{\mathrm{range}\,}$$

$$\newcommand{\RealPart}{\mathrm{Re}}$$ $$\newcommand{\ImaginaryPart}{\mathrm{Im}}$$

$$\newcommand{\Argument}{\mathrm{Arg}}$$ $$\newcommand{\norm}[1]{\| #1 \|}$$

$$\newcommand{\inner}[2]{\langle #1, #2 \rangle}$$

$$\newcommand{\Span}{\mathrm{span}}$$

$$\newcommand{\id}{\mathrm{id}}$$

$$\newcommand{\Span}{\mathrm{span}}$$

$$\newcommand{\kernel}{\mathrm{null}\,}$$

$$\newcommand{\range}{\mathrm{range}\,}$$

$$\newcommand{\RealPart}{\mathrm{Re}}$$

$$\newcommand{\ImaginaryPart}{\mathrm{Im}}$$

$$\newcommand{\Argument}{\mathrm{Arg}}$$

$$\newcommand{\norm}[1]{\| #1 \|}$$

$$\newcommand{\inner}[2]{\langle #1, #2 \rangle}$$

$$\newcommand{\Span}{\mathrm{span}}$$ $$\newcommand{\AA}{\unicode[.8,0]{x212B}}$$

$$\newcommand{\vectorA}[1]{\vec{#1}} % arrow$$

$$\newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow$$

$$\newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} }$$

$$\newcommand{\vectorC}[1]{\textbf{#1}}$$

$$\newcommand{\vectorD}[1]{\overrightarrow{#1}}$$

$$\newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}}$$

$$\newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}}$$

$$\newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} }$$

$$\newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}}$$

Many of MATLAB's built-in functions work with vectors. In fact, any built-in function that makes sense to work with vectors should work with vector inputs and/or outputs. And any built-in function that makes sense to work with matrices should work with matrix inputs and/or outputs.

Here are some common examples of using MATLAB functions that work with vector inputs:

x1 = 5
x2 = 0: 2: 8 % [0 2 4 6 8]
m2 = [1 3 5
4 2 -1];

%% sqrt(x)
sqrt(x1) % 2.2361
sqrt(x2) % 0 1.4142 2.0000 2.4495 2.8284
sqrt(m2) % 1.00+0i 1.7321+0i 2.2361+0i
% 2.00+0i 1.4142+0i 0.0000+1.0i

%% rem(n,d) % remainder of n divided by d
rem(x1,3) % 2 (remainder of 5 divided by 3)
rem(x2,3) % 0 2 1 0 2
rem(m2,3) % 1 0 2
% 1 2 -1

mod(x1,3) % 2 (remainder of 5 divided by 3)
mod(x2,3) % 0 2 1 0 2
mod(m2,3) % 1 0 2
% 1 2 2

%% size(x)
size(x1) % 1 1 (1 row, 1 column)
size(x2) % 1 5 (1 row, 5 columns)
size(m2) % 2 2 (2 rows, 3 columns)
[ncols, nrows] = size(m2)
% ncols = 2
% nrows = 3

%% sin(x) in radians
% cos(x), tan(x) work the same way
sin(x1) % -0.9589
sin(x2) % 0 0.9093 -0.7568 -0.2794 0.9894
sin(m2) % 0.8415 0.1411 -0.9589
% -0.7568 0.9093 -0.8415

%% exp(x) = exponential of e^x, (e = 2.7183)
exp(x1) % 148.4132
exp(x2) % 0.0010 0.0074 0.0546 0.4034 2.9810
exp(m2) % 2.7183 20.0855 148.4132
% 54.5982 7.3891 0.3679

%% log(x) = the natural logarithm (base e)
% In math and physics, this is called ln(x)
% But log(x) = the natural logarithm has been traditional
% in many computer programming languages since the 1960s
log(x1) % 1.6094
log(x2) % -Inf 0.6931 1.3863 1.7918 2.0794
log(m2) % 0+0i 1.098+0i 1.609+0i
% 1.38+0i 0.693+0i 0.000+3.1416i
% In Matlab, the log base 10 function is log10():
log10(x2) % -Inf 0.3010 0.6021 0.7782 0.9031

%% You can also nest functions, as shown here.
sin(sqrt(0.5)) % 0.6496 MATLAB fist computes sqrt(0.5), then computes the sin() of that result.

.

This page titled 8.1.1: Built-in MATLAB Functions of Vectors is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Carey Smith.