Mathematical Functions

 
cos(b)
sin(b)
tan(b)
log(b)
log10(b)
log2(b)
exp(b)
abs(b)
sum(A)
prod(A)
max(A)
min(A)
floor(A)
ceil(A)
round(A)
sqrt(b)
nthroot(b,n)

Apart from scalars, Matlab mathematical functions can also take vectors and matrices as inputs. If input to a Matlab function is a matrix, the output is also a matrix of the same size, i.e., the operation is performed element-wise.


max(A)        % Default: column-wise max
max(A,[],1)   % column-wise max (maximum along 1st dimension)
max(A,[],2)   % row-wise max (maximum along 2nd dimension)
max(A,B)      % element-wise max
% max of all elements of a matrix
max(max(A))
or
max(A(:))
max([1 2; 3 4; 5 6])
>>  5   6       % (column-wise max)

min([1 4 5; 2 3 6])
>>  1   3   5   % (column-wise min)
sum(A)      % Default: column-wise sum
sum(A,1)    % column-wise sum (sum along 1st dimension)
sum(A,2)    % row-wise sum (sum along 2nd dimension)
V = [-1 -2 5 0 3];

abs(V)
>>  1   2   5   0   3

max(V)
>>  5

min(V)
>>  -2
[value1, index1] = max(V)
>>
  value1 =  5
  index1 =  3

[value2, index2] = min(V)
>>
  value2 =  -2
  index2 =  2

exp(1)
>>  2.7183

exp(1:3)
>>  2.7183    7.3891   20.0855
log(2.7)
>>  0.99325

log(-1)
>>  0.00000 + 3.14159i

log(1:3)
>>  0.00000   0.69315   1.09861