FUNCTIONS AND CODES MATLAB
INTRODUCTION TO MATLAB
Getting information
help elfun Help, followed by the function you
want to search for, to get some
more information about that
specific function.
whos Shows the variables that are in the
workplace, suppling their name,
size, bytes, class and attributes
size(A) Give dimensions (i.e. number of
rows and columns) of your matrix
A.
find The find function converts a logical
F = find(D) index into a subscript index by
returning the positions of all
nonzero (or true) elements in the
logical array.
Basic Syntax
x = pi Create variables and assign values with the equal sign (=).
The left side (x) is the variable name, and the right side (pi)
is its value.
Matrices are denoted by upper case letters whereas vector
and scalars are denoted by lower case letters.
y = sin (- Provide inputs to a function using parentheses.
5)
Desktop Management
save save data.mat Save your current workspace
to a MAT-file
load load data.mat Load the variables in a MAT-
file to the workspace.
clear clear Clear all variables from the
clear A workspace.
Clear variable A.
clc clc Clear all text from the
Command Window.
format format long Change how numeric output
format short appears in the Command
Window.
close all close all Close all figures
which which(‘Tutorial_part6_withans From where matlab is running
wers’) a certain command
dlmwrit dlmwrite(‘my.data.txt’, data, ‘\ Save stuff in ASCII text format
e t’) to load into SPSS or Excel
,Array Types
4 Scalar
[3 5 8 10] Row vector; row of numbers (one-
dimensional array)
[1;3;5;6] Colum vector; column of numbers
(one-dimensional array). A
semicolon between the squared
brackets basically means: "start a
new row".
[3 4 5; 6 7 8] Matrix; a rectangular array of
numbers (two-dimensional array).
Commas or spaces to separate the
columns within a row and use
semicolons to start new rows.
M_addition = [1, 15, 18] Create an extra row.
M_new = [M; M_addition] Add extra row to original matrix M.
new_row = [30 31 32 33 34] A = magic (5)
A = [A; new_row] A(6,:) = new_row
g = [a(:,1),b(:,4)] g is column 1 of a next to column 4
of b
Evenly Spaced Vectors
1:4 Create a vector from 1 to 4, spaced by 1, using
the colon operator (:).
1:0.5:4 Create a vector from 1 to 4, spaced by 0.5.
linspace(1,10,5) Create a vector with 5 elements. The values are
evenly spaced from 1 to 10.
Matrix and Vector Creation
rand(2) Create a square matrix with 2 rows and 2 columns.
zeros(2,3) Create a rectangular matrix with 2 rows and 3 columns of
0s.
ones(2,3) Create a rectangular matrix with 2 rows and 3 columns of
1s.
zeros() Creates an array of zeros.
ones() Creates an array of ones.
randn() Generates random numbers with a normal distribution (mean
0 and standard deviation 1), where numbers can be either
positive or negative
rand() rand(3,5)
Create a matrix of uniformly distributed random numbers
between 0 and 1 with 3 rows and 5 columns
repmat() Replicates and tiles an array to create a larger array.
B = repmat(A, m, n)
Replicates matrix A m times in the row dimension and n times
in the column dimension
, linspace Generates linearly spaced vectors. It creates an array of
() evenly spaced numbers between two specified values
L = linspace(a, b, n)
Generates n points between a and b
logspace Generates logarithmically spaced vectors. It creates an array
() of numbers spaced evenly on a log scale between two
specified powers of 10.
L = logspace(p1, p2, n)
Generates n points between 10^p1 and 10^p2
Array Indexing
A(end,2) Access the element in the second column of the last
row.
A(2,:) Access the entire second row.
A(1:3,:) Access all colums of the first three rows.
A(2) = 11 Change the value of the second element of an array to
11.
A(:,4)=0 Change all values in column 5 to zero.
Array Operations
[1 2; 3 4] + 1 Perform array addition.
ans =
2 3
4 5
[1 1; 1 1]*[2 2; 2 2] Perform matrix multiplication.
ans =
4 4
4 4 Each row of the first matrix is multiplied
by each column of the second matrix
according to the rules of linear algebra.
[1 1; 1 1].*[2 2; 2 2] Perform element-wise multiplication.
ans =
2 2
2 2
Operators
.* Means element-wise multiplication
To perform an element-wise operation, the arrays involved
must be the same size, or one must be scalar (a single value
that applies to all elements of the other array).
./ Means element-wise division
.^ Means element-wise power
* Means matrix multiplication
INTRODUCTION TO MATLAB
Getting information
help elfun Help, followed by the function you
want to search for, to get some
more information about that
specific function.
whos Shows the variables that are in the
workplace, suppling their name,
size, bytes, class and attributes
size(A) Give dimensions (i.e. number of
rows and columns) of your matrix
A.
find The find function converts a logical
F = find(D) index into a subscript index by
returning the positions of all
nonzero (or true) elements in the
logical array.
Basic Syntax
x = pi Create variables and assign values with the equal sign (=).
The left side (x) is the variable name, and the right side (pi)
is its value.
Matrices are denoted by upper case letters whereas vector
and scalars are denoted by lower case letters.
y = sin (- Provide inputs to a function using parentheses.
5)
Desktop Management
save save data.mat Save your current workspace
to a MAT-file
load load data.mat Load the variables in a MAT-
file to the workspace.
clear clear Clear all variables from the
clear A workspace.
Clear variable A.
clc clc Clear all text from the
Command Window.
format format long Change how numeric output
format short appears in the Command
Window.
close all close all Close all figures
which which(‘Tutorial_part6_withans From where matlab is running
wers’) a certain command
dlmwrit dlmwrite(‘my.data.txt’, data, ‘\ Save stuff in ASCII text format
e t’) to load into SPSS or Excel
,Array Types
4 Scalar
[3 5 8 10] Row vector; row of numbers (one-
dimensional array)
[1;3;5;6] Colum vector; column of numbers
(one-dimensional array). A
semicolon between the squared
brackets basically means: "start a
new row".
[3 4 5; 6 7 8] Matrix; a rectangular array of
numbers (two-dimensional array).
Commas or spaces to separate the
columns within a row and use
semicolons to start new rows.
M_addition = [1, 15, 18] Create an extra row.
M_new = [M; M_addition] Add extra row to original matrix M.
new_row = [30 31 32 33 34] A = magic (5)
A = [A; new_row] A(6,:) = new_row
g = [a(:,1),b(:,4)] g is column 1 of a next to column 4
of b
Evenly Spaced Vectors
1:4 Create a vector from 1 to 4, spaced by 1, using
the colon operator (:).
1:0.5:4 Create a vector from 1 to 4, spaced by 0.5.
linspace(1,10,5) Create a vector with 5 elements. The values are
evenly spaced from 1 to 10.
Matrix and Vector Creation
rand(2) Create a square matrix with 2 rows and 2 columns.
zeros(2,3) Create a rectangular matrix with 2 rows and 3 columns of
0s.
ones(2,3) Create a rectangular matrix with 2 rows and 3 columns of
1s.
zeros() Creates an array of zeros.
ones() Creates an array of ones.
randn() Generates random numbers with a normal distribution (mean
0 and standard deviation 1), where numbers can be either
positive or negative
rand() rand(3,5)
Create a matrix of uniformly distributed random numbers
between 0 and 1 with 3 rows and 5 columns
repmat() Replicates and tiles an array to create a larger array.
B = repmat(A, m, n)
Replicates matrix A m times in the row dimension and n times
in the column dimension
, linspace Generates linearly spaced vectors. It creates an array of
() evenly spaced numbers between two specified values
L = linspace(a, b, n)
Generates n points between a and b
logspace Generates logarithmically spaced vectors. It creates an array
() of numbers spaced evenly on a log scale between two
specified powers of 10.
L = logspace(p1, p2, n)
Generates n points between 10^p1 and 10^p2
Array Indexing
A(end,2) Access the element in the second column of the last
row.
A(2,:) Access the entire second row.
A(1:3,:) Access all colums of the first three rows.
A(2) = 11 Change the value of the second element of an array to
11.
A(:,4)=0 Change all values in column 5 to zero.
Array Operations
[1 2; 3 4] + 1 Perform array addition.
ans =
2 3
4 5
[1 1; 1 1]*[2 2; 2 2] Perform matrix multiplication.
ans =
4 4
4 4 Each row of the first matrix is multiplied
by each column of the second matrix
according to the rules of linear algebra.
[1 1; 1 1].*[2 2; 2 2] Perform element-wise multiplication.
ans =
2 2
2 2
Operators
.* Means element-wise multiplication
To perform an element-wise operation, the arrays involved
must be the same size, or one must be scalar (a single value
that applies to all elements of the other array).
./ Means element-wise division
.^ Means element-wise power
* Means matrix multiplication