- 1. Sum(a)
- 2. A’
- 3. Sum(a’)’
- 4. Diag(A)
- 5. Fliplr(a)
- 6. Diag(fliplr(a))
- 7. A(4,2)=a(8)
- 8. To add an element,
- 9. A(4,5)=17; than 17= a(20)
10. 1:10 = 1 2 3 4 5 6 7 8 9 10
11. A(1:3,4) refers to first 3 elements of 4th column of matrix (a)
12. A(:,end) refers to sum of all the elements of last column of a
13. 0 : pi/4 : 2*pi
ans = 0 0.7854 1.5708 2.3562 3.1416 3.9270 4.7124 5.4978 6.2832
14. Magic function creates a square matrix automatically that has magic numbers, i.e all its rows and columns add upto same no., eg
B = magic(4)
B =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
15. Swapping of 2 columns of a matrix b, a = b(:,[1 3 2 4])
A =
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1
This is Durer’s engraving matrix
16. Elementary matrices
zeros - Zeros array.
ones - Ones array.
eye - Identity matrix.
repmat - Replicate and tile array.
rand - Uniformly distributed random numbers.
randn - Normally distributed random numbers.
linspace - Linearly spaced vector.
logspace - Logarithmically spaced vector.
freqspace - Frequency spacing for frequency response.
meshgrid - X and Y arrays for 3-D plots.
accumarray - Construct an array with accumulation.
: - Regularly spaced vector and index into matrix.
17. Basic array information
size - Size of array.
length - Length of vector.
ndims - Number of dimensions.
numel - Number of elements.
disp - Display matrix or text.
isempty - True for empty array.
isequal - True if arrays are numerically equal.
isequalwithequalnans - True if arrays are numerically equal.
- 18. Matrix manipulation
cat - Concatenate arrays.
reshape - Change size.
diag - Diagonal matrices and diagonals of matrix.
blkdiag - Block diagonal concatenation.
tril - Extract lower triangular part.
triu - Extract upper triangular part.
fliplr - Flip matrix in left/right direction.
flipud - Flip matrix in up/down direction.
flipdim - Flip matrix along specified dimension.
rot90 - Rotate matrix 90 degrees.
: - Regularly spaced vector and index into matrix.
find - Find indices of nonzero elements.
end - Last index.
sub2ind - Linear index from multiple subscripts.
ind2sub - Multiple subscripts from linear index.
19. Multi-dimensional array functions
ndgrid - Generate arrays for N-D functions and interpolation.
permute - Permute array dimensions.
ipermute - Inverse permute array dimensions.
shiftdim - Shift dimensions.
circshift - Shift array circularly.
squeeze - Remove singleton dimensions.
20. Array utility functions
isscalar - True for scalar.
isvector - True for vector.
21. Specialized matrices
compan - Companion matrix.
gallery - Higham test matrices.
hadamard - Hadamard matrix.
hankel - Hankel matrix.
hilb - Hilbert matrix.
invhilb - Inverse Hilbert matrix.
magic - Magic square.
pascal - Pascal matrix.
rosser - Classic symmetric eigenvalue test problem.
toeplitz - Toeplitz matrix.
vander - Vandermonde matrix.
wilkinson - Wilkinson's eigenvalue test matrix.
EXPRESSIONS
Like most other programming languages, MATLAB provides mathematical expressions, but unlike most programming languages, these expressions involve entire matrices. The building blocks of expressions are
Variables, Numbers, Operators, Functions
MATLAB is case sensitive
- Variable: num_students = 25 creates a 1-by-1 matrix named num_students and stores the value 25 in its single element
- Numbers: MATLAB uses conventional decimal notation, with an optional decimal point and leading plus or minus sign, for numbers. Scientific notation uses the letter e to specify a power-of-ten scale factor. Imaginary numbers use either i or j as a suffix. Some examples of legal numbers are
3 -99 0.0001
9.6397238 1.60210e-20 6.02252e23
1i -3.14159j 3e5i
- Operators: Expressions use familiar arithmetic operators and precedence rules.
+ Addition
- Subtraction
* Multiplication
/ Division
\ Left division (described in "Matrices and Linear Algebra" in the MATLAB documentation)
^ Power
' Complex conjugate transpose
( ) Specify evaluation order
OPERATORS
Converting a character value to hexadecimal value, we use double:
Eg. S = ‘ A B C D’
X = double(S)
Returns:
X = 65 32 66 32 ( ASCII characters of A B C and D)
And to convert numbers to strings, use keyword: char
Eg. X = 65
S = char(x)
Returns S = ‘A’
Arithmetic Operations:
+Plus
-Minus
.Decimal point
=Assignment
*Matrix multiplication
/Matrix right division
\Matrix left division
^Matrix power
'Matrix transpose
.*Array multiplication (element-wise)
./Array right division (element-wise)
.\Array left division (element-wise)
.^Array power (element-wise)
.'Array transpose
Bitwise Operations
Bitand Bit-wise AND
Bitcmp Bit-wise complement
Bitor Bit-wise OR
bitmax Maximum floating-point integer
bitset Set bit at specified position
bitshift Bit-wise shift
bitget Get bit at specified position bit
xor Bit-wise XOR
Relational Operators
< Less than
> Greater than
>= Greater than or equal to
== Equal to
~= Not equal to
LOGICAL OPERATORS
&& Logical AND
|| Logical OR
& Logical AND for arrays
| Logical OR for arrays
~ Logical NOT
all Test to determine if all elements are nonzero
any Test for any nonzero elements
false False array
find Find indices and values of nonzero elements
is* Detect state
isa Determine if item is object of given class
iskeyword Determine if string is MATLAB keyword
isvarname Determine if string is valid variable name
logical Convert numeric values to logical
true True array
xor Logical EXCLUSIVE OR
Elementary math functions
- 1. Trigonometric.
sin - Sine
sind - Sine of argument in degrees.
sinh - Hyperbolic sine.
asin - Inverse sine.
asind - Inverse sine, result in degrees.
asinh - Inverse hyperbolic sine.
cos - Cosine.
cosd - Cosine of argument in degrees.
cosh - Hyperbolic cosine.
acos - Inverse cosine.
acosd - Inverse cosine, result in degrees.
acosh - Inverse hyperbolic cosine.
tan - Tangent.
tand - Tangent of argument in degrees.
tanh - Hyperbolic tangent.
atan - Inverse tangent.
atand - Inverse tangent, result in degrees.
atan2 - Four quadrant inverse tangent.
atanh - Inverse hyperbolic tangent.
sec - Secant.
secd - Secant of argument in degrees.
sech - Hyperbolic secant.
asec - Inverse secant.
asecd - Inverse secant, result in degrees.
asech - Inverse hyperbolic secant.
csc - Cosecant.
cscd - Cosecant of argument in degrees.
csch - Hyperbolic cosecant.
acsc - Inverse cosecant.
acscd - Inverse cosecant, result in degrees.
acsch - Inverse hyperbolic cosecant.
cot - Cotangent.
cotd - Cotangent of argument in degrees.
coth - Hyperbolic cotangent.
acot - Inverse cotangent.
acotd - Inverse cotangent, result in degrees.
acoth - Inverse hyperbolic cotangent.
- 2. Exponential.
exp - Exponential.
expm1 - Compute exp(x)-1 accurately.
log - Natural logarithm.
log1p - Compute log(1+x) accurately.
log10 - Common (base 10) logarithm.
log2 - Base 2 logarithm and dissect floating point number.
pow2 - Base 2 power and scale floating point number.
realpow - Power that will error out on complex result.
reallog - Natural logarithm of real number.
realsqrt - Square root of number greater than or equal to zero.
sqrt - Square root.
nthroot - Real n-th root of real numbers.
nextpow2 - Next higher power of 2.
- 3. Complex
abs - Absolute value.
angle - Phase angle.
complex - Construct complex data from real and imaginary parts.
conj - Complex conjugate.
imag - Complex imaginary part.
real - Complex real part.
unwrap - Unwrap phase angle.
isreal - True for real array.
cplxpair - Sort numbers into complex conjugate pairs.
- 4. Rounding and remainder
fix - Round towards zero.
floor - Round towards minus infinity.
ceil - Round towards plus infinity.
round - Round towards nearest integer.
mod - Modulus (signed remainder after division).
rem - Remainder after division.
sign - Signum.
- 5. Specialized math functions
airy - Airy functions.
besselj - Bessel function of the first kind.
bessely - Bessel function of the second kind.
besselh - Bessel functions of the third kind (Hankel function).
besseli - Modified Bessel function of the first kind.
besselk - Modified Bessel function of the second kind.
beta - Beta function.
betainc - Incomplete beta function.
betaln - Logarithm of beta function.
ellipj - Jacobi elliptic functions.
ellipke - Complete elliptic integral.
erf - Error function.
erfc - Complementary error function.
erfcx - Scaled complementary error function.
erfinv - Inverse error function.
expint - Exponential integral function.
gamma - Gamma function.
gammainc - Incomplete gamma function.
gammaln - Logarithm of gamma function.
psi - Psi (polygamma) function.
legendre - Associated Legendre function.
cross - Vector cross product.
dot - Vector dot product.
- 6. Number theoretic functions
factor - Prime factors.
isprime - True for prime numbers.
primes - Generate list of prime numbers.
gcd - Greatest common divisor.
lcm - Least common multiple.
rat - Rational approximation.
rats - Rational output.
perms - All possible permutations.
nchoosek - All combinations of N elements taken K at a time.
factorial - Factorial function.
- 7. Coordinate transforms.
cart2sph - Transform Cartesian to spherical coordinates.
cart2pol - Transform Cartesian to polar coordinates.
pol2cart - Transform polar to Cartesian coordinates.
sph2cart - Transform spherical to Cartesian coordinates.
hsv2rgb - Convert hue-saturation-value colors to red-green-blue.
rgb2hsv - Convert red-green-blue colors to hue-saturation-value.
- 8. Special variables and constants
ans - Most recent answer.
eps - Floating point relative accuracy.
realmax - Largest positive floating point number.
realmin - Smallest positive floating point number.
pi - 3.1415926535897....
i, j - Imaginary unit.
inf - Infinity.
NaN - Not-a-Number.
isnan - True for Not-a-Number.
isinf - True for infinite elements.
isfinite - True for finite elements.
why - Succinct answer.