Matlab Notes
Matlab Notes
MATLAB Notes
@Soumil Gupta
Basic Commands →
type whos to see the detailed list of all variables right on the command
window. notably, it shows the type of the variable also. alternatively,
double click on the variable on the workspace to get more information
about that variable (helpful with matrices)
type var = ‘hello’ to create a string in MATLAB. the data type of this
var is char .
type var = "hello" to create a string in MATLAB. the data type of this
var is string.
press the up arrow on your keyboard to see the list of all the previous
commands you’ve run.
MATLAB Notes 1
A script is different from the command window. It is a like coding a C
file.
Keep in mind that if you don’t want a variable to be printed when the
program is run, then use the ; operator at the end of the line.
x = 1:1:10 creates a horizontal array with elements starting from 1, with
a jump value of 1and ending at 10.
To change the dimension of the array (i.e. transpose), use the operator
‘ e.g.: x’
All Linear Algebra Rules are followed by MATLAB. e.g, A+2 adds 2 to all
the elements of the matrix A.
Element Wise Operation: What if you want to run operations not on the
matrix itself but on the elements of the matrix, for example, you want to
square all the elements of a matrix A: type A.^2 . The . operator refers
to the element-wise operations.
MATLAB Notes 2
Make Zero Matrix: O = zeros(3). For custom size: O = zeros(3,1) , this
gives a 3row by 1column matrix with all entries equal to 0.
1.
Make Identity Matrix: I = eye(3)
Access elements of a matrix the same way you did in C, but use ()
instead of [] . e.g, A(2,3) . For a 1D array you just to have to specify one
index. You are free to change the value at any time by accessing the
element.
A(end-10) gives you the 10thelement from the end of the array A.
Extract entire row/column from a matrix: To extract entire row, e,g: A(2,
A(2,1:2) gives all the element of second row from first to second
column.
Math Functions →
click on the fx option to get the list of all math functions available on
MATLAB
type help function_name to get all the required parameters for the
function.
use plot(x, y) to plot the graph. the first parameter will be on the x-axis
and the second parameter will be on the y-axis.
x = linspace(0,5);
y=(-(x-3).^2)+10;
plot(x,y);
max(y) %gives the maximum value of y
min(y) %gives the minimum value of y
MATLAB Notes 3
MATLAB Notes 4