MatLab (MATrix LABoratory)
MATLAB is a high-level language and interactive environment built for numerical computing, matrix operations, data analysis, visualization, and algorithm development.
It is widely used in engineering, research, AI/ML, signal processing, control systems, and finance.
Learning Path
- MATLAB On Ramp
- Deep Learning On Ramp
- Machine Learning on Ramp
- Reinforced Lerning on Ramp
- Fundamental Of Programming
Finding Help
doc fn = document for function
% To add comment
pwd:to get current directorycd: change directoryls: list directorywho: view variable in current scopewhos: Detailed view of variable sin scopeclear: clear variables in current workspace
Data Types
Numeric Types
Integer and floating-point data
- unsigned Int:
uint8/16/32/64 - signed Int:
int8/16/32/64 - single : 4 byte float
- double : 8 byte double precision
Characters and Strings
Text in character arrays and string arrays
Dates and Time
Arrays of date and time values that can be displayed in different formats
Categorical Arrays
Arrays of qualitative data with values from a finite set of discrete, nonnumeric data
Tables
Arrays in tabular form whose named columns can have different types
Timetables
Time-stamped data in tabular form
Structures
Arrays with named fields that can contain data of varying types and sizes
Cell Arrays
Arrays that can contain data of varying types and sizes
Function Handles
Variables that allow you to invoke a function indirectly
Map Containers
Objects with keys that index to values, where keys need not be integers
Time Series
Data vectors sampled over time
Precision of Variables in Workspace
format long // Upto 10+ decimal places
format short // Upto 4 decimal places
Variable:
- starts with alphabets
- contain a_z, A_Z , 0..9, & _
- MatLab automatically show suggestion for incorrect variables
- Can see data value by entering variable name
Saving & Loading Variables
Save all Variables in MAT File
save <filename>.mat
Save Single variable in MAT file
save <filename>.mat <variableName>
Save all Variables in Readable File format
save <filename>.txt -ascii
Load Saved Variables
load <filename>.mat
load <filename>.dat
Load Single Saved Variables
load <filename>.mat <variableName>
Empty WorkSpace
clear
Creating Arrays & Matrix
magic(n,m) sum of rows, column , diagonal is same
x = [7 9] // row array or row vector [element element]
x= x' // Transpose into column vector
( A')'==A // Double transpose is same
x = start:end // Shortcut create Row Vector [1,2,3,4]
x= start: step: step // shortcut create Row Vector 1 to 5 step 0.5 [1,1.5,2,2.5, 3,3.5,4,4.5 5]
x= (start: step: step)' // shortcut create Column Vector 1 to 5 step 0.5
x = linspace(start ,end ,space) // equidistance rowvector
x = linspace(start ,end ,space)' // equidistance columnn vector
- , Rows
- ; Column
x = [7,9] // row array [element , element]
x = [7;9] // column array [element ; element]
x = [4,5 ; 7,9] // 2d Matrix [ROW; ROW]
x = rand(n) // Generate nxn matrix with random number
x = rand(n,m) // Generate nxm matrix with random number
x = zeros(n,m) // Generate nXm matrix with zero elements
x = ones(n,m) // Generate nxm unit matrix
x= eye(n,m) // GenerateIdentity Matrix
x= eye(n) // Generate nxn Identity Matrix
Tricks
A.*eye(n) = diagonal matrix of A
Reading Values from Matrix
- Indexing starts with 1
length(V) // Length of Vector
size(A) // size of matrix
[dr, dc] = size(A) // Size in dr,dc
[vMax, ivMax]= max(A) // Max Value and its Index
A(:) // convert Matrix into single column Vector
A(i) // ith element of Vector
A(n:m) // All elements from n to m index of Vector
A(i,j) // ith row, jth column of Matrix
A(end,j) // last row j column
A(n, :) // Get all elements of nth Row
A(:, n) // Get all elements of nth Column
A(:, end-1:end) // all elements of last 2 column
A([a,b], :) // all elements of a, b rows
Tricks
[vMax, ivMax]= max(A) // Max Value and its Index
max(A,[],1) // return max per column
max(A,[],2) // return max per row
max(A(:)) // max eleemnt in A
sum(A,1) // per column sum
sum(A,1) // per row sum
A.*eye(n) = diagonal matrix of A
sum(sum(A.*eye(n))) = sum of all diagonal elements of A
Update Values in Matrix
A(i) = b // modify A(i)th element to b
A(i,j) = b // modify A(i,j)th element to b
C =[A,B] = [A B] // concate B after last column of A
C =[A;B] // concate B below last row of A
A =[A,[a;b;c]] // append a,b,c as last column in A
- Operation on Matrix applies on all elements of matrix
A + b // Add Scaler b to all elements of Matrix A
A / b // Divide Scaler b to all elements of Matrix A
A<3 // element wise compare and return binary Matrix
[r,c] = find(A<3) // return row and column index matrix(r & c) satisfying
A + B // Add A+ B
A.*B // Multiply A.*B
Functions
fn(A) // Math fn on all elements of Matrix A eg sqrt, abs, log
sum(A) // Add up all elemnts
prod(A) // Return product of all elements
floor(A) // floor (0,5-> 0)
ceil(A) // round (0,5->1)
Inverse
pinv(A) //Inverse of Matrix
pinv(A)*A // Identity Matrix of A
