MATLAB Notes1
MATLAB Notes1
Introduction to Matlab
The basic MATLAB interface.
Basic commands.
Declaring & manipulating variables.
Plotting graphs.
Conditional Operators.
Functions.
What is Matlab?
The name MATLAB stands for matrix
laboratory.
MATLAB was originally written to provide
easy access to matrix software developed
by the LINPACK and EISPACK projects,
which together represent the state-of-the-art
in software for matrix computation.
MATLAB is a numerical computing
environment and fourth generation
programming language. Developed by The
MathWorks
Matlab had many functions and toolboxes
to help in various applications
It allows you to solve many technical
computing problems, especially those with
matrix and vector formulas, in a fraction of
the time it would take to write a program in
a scalar non-interactive language such as
C or Fortran.
What is Matlab?
MATLAB is a high-performance language for technical
computing.
It integrates computation, visualization, and programming
in an easy-to-use environment where problems and
solutions are expressed in familiar mathematical notation.
Typical uses include:
Algorithm development
Math and computation
Modeling, simulation, and prototyping
Data analysis, exploration, and visualization
Scientific and engineering graphics
Application development, including Graphical User
Interface building.
Matlab Programming Environment
Matlab (Matrix Laboratory) is a
dynamic, interpreted,
environment for matrix/vector
analysis
Variables are created at run-time,
matrices are dynamically re-sized,
…
User can build programs (in .m
files or at command line) using a
C/Java-like syntax
Ideal environment for model
building, system identification and
control (both discrete and
continuous time)
Wide variety of libraries
(toolboxes) available
Matlab Programming Environment
Variable Command
Click on the Matlab window
icon/start menu browser
initialises the Matlab
environment:
The variable
browser shows
which variables Command history
currently exist in the
The MATLAB System
MATLAB system consists of these main
parts:
Desktop Tools and Development Environment
Includes the MATLAB desktop and Command
Window, an editor and debugger, a code analyzer,
browsers for viewing help, the workspace, files, and
other tools
Mathematical Function Library
vast collection of computational algorithms ranging
from elementary functions, like sine, cosine, and
complex arithmetic, to more sophisticated functions
like matrix inverse, matrix eigenvalues, Bessel
functions, and fast Fourier transforms.
The Language
The MATLAB language is a high-level matrix/array
language with control flow statements, functions, data
structures, input/output, and object-oriented programming
features.
Graphics
MATLAB has extensive facilities for displaying vectors and
matrices as graphs, as well as editing and printing these
graphs. It also includes functions that allow you to
customize the appearance of graphics as well as build
complete graphical user interfaces on your MATLAB
applications.
External Interfaces
The external interfaces library allows you to write C and
Fortran programs that interact with MATLAB.
Main Matlab Window
Basic MATLAB
Interface
Command window: Type your
instructions here and press
ENTER to execute them.
Example: Declare a column matrix with
values 1,2 and 3.
Command history: a list of instructions
executed by MATLAB is shown here.
Workspace: shows a list of
variables created by MATLAB.
As you can see, the value of ‘aaa’
is shown.
Another way to create a variable
Is to press this button.
MATLAB will prompt you to enter
the variable name.
As you can see, the variable
name has been changed to bbb.
2) Or by double clicking
on bbb.
1 2
A
3 4
Try It Yourself
Create this matrix:
2 4 5
matrixB 2 4 5
5 3 5
Accessing Matrix Values
To access a specific value inside a matrix,
use this command:
matrixName(rowNumber, colNumber)
Example: to access a value inside row 3
and column 2.
matrixName(3,2)
Try It Yourself
Create this matrix:
3 4 5
matrixB 8 9 10
6 7 1
3 4 5
matrixB 8 9 10
6 7 1
Get all the values from row 3 and save it into a new
variable.
Get all the values from column 1 and save it into a new
variable.
Creating a Matrix of Zeros
To create a matrix of zeros, use the zeros
command.
Example: create a 6 X 5 matrix of zeros.
zeros(6,5)
Example
Creating a Matrix of Ones
To create a matrix of ones, use the ones
command.
Example: create a 5 X 3 matrix of ones.
ones(5,3)
Example
Creating a Matrix of Random
Numbers
To create a matrix of random numbers,
use the rand command.
Example: create a 4 X 4 matrix of random
numbers.
rand(4,4)
Example
Getting the Size of the Matrix
To get the size of the matrix, use the size
command.
Example: to get the size of matrix aaa.
[numRow, numCol] = size(aaa);
Example
Transposing a Matrix
A transpose operation changes the
column of a matrix into rows, and rows into
columns.
To do a transpose, use the single quote
operator.
Example: Transposing a Row
Matrix
Example: Transposing a Column
Matrix
Example: Transposing a Regular
Matrix
Finding the Maximum Value
To find the maximum value for a matrix,
use the max function.
Example: find the maximum value in
matrix aaa.
maxVal = max(aaa);
Example
xxx = 1:100
yyy = rand(1,100)
plot(xxx,yyy)
Example
Example: Draw sin(x)
clear, close all
clc
x = 0:pi/36:10*pi
y = sin(x)
plot(x,y,‘m')
Example
Plot Styles
Plot Styles
Example: Plotting the lines using
line parameters
clear, close all
clc
xxx = 1:100
yyy = rand(1,100)
plot(xxx,yyy)
figure, plot(xxx,yyy,'g:') % the command figure is
figure, plot(xxx,yyy,'r--')% used to create a new
figure, plot(xxx,yyy,':mo')% figure each time a plot
% is made.
Example: Drawing two plots in the
same figure
clear, close all
clc
xxx = 1:100
yyy = rand(1,100)
aaa = 1:100
bbb = rand(1,100)
plot(xxx,yyy,'r')
hold on
plot(aaa,bbb,'-.m')
hold off
Example: Drawing Bar Graphs
clear, close all
clc
x = 0:pi/36:2*pi
y = cos(x)
bar(x,y,‘r')
Example
Example: Drawing a Stair-Step Plot
x = -10:0.5:10
y = x.^2 + 2.*x + 2
stairs(x,y,'b')
Example
Image Manipulation
Reading and Displaying an Image
aaa = rand(1,100);
bbb = 1:1:100
color = 1;
if (color == 1)
% if block
figure, plot(bbb,aaa,':r');
else
% else block
figure, plot(bbb,aaa,'b');
end
Example
clear, close all
clc
x = 3;
if (x > 5)
disp('The number is more than 5.')
elseif (x == 5)
disp('The number is equal to 5.')
else
disp('The number is less than 5.')
end
For loop
Used to repeat a set of statements
multiple times.
The for loop format is:
for(startingvalue:increment:endingvalue)
For Loop
end
Example: Display value inside for
loop
clear, close all
clc
for i = 1:1:15
st1 = strcat('The value of i inside the
loop is: ',int2str(i));
disp(st1)
end
Example: Display “Hello World” 10
Times
st1 = 'Hello World!';
for i = 1:1:10
disp(st1)
end
Example: Check Value Inside
Matrix
clear, close all
clc
for i = 1:1:row
for j = 1:1:col
currNo = matA(i,j);
st1 = strcat('The value being tested is: ', num2str(currNo),'.');
disp(st1)
if (currNo > 3)
disp('The current value is larger than 3.')
else
disp('The current value is less or equal than 3.')
end
end
end
While loop
Used to repeat a set of statements while
the tested condition is true.
The while loop format is:
while(condition)
counter = 1;
number1 = 4;
number2 = 5;
selection = 1;
if selection == 1
hasil = addFcn(number1,number2);
elseif selection == 2
hasil = subFcn(number1,number2);
elseif selection == 3
hasil = mulFcn(number1,number2);
elseif selection == 4
hasil = divFcn(number1,number2);
else
disp('The selection is invalid.')
end
calcSphereVolume.
The formula for volume
3
is:
Problem 2
Write a program that plots the function:
y x x 3x 6
3 2
for values of x = -50 to 50
Solutions: x = -50:1:50;
y = x.^3+x.^2+3*x+6
plot(x,y)
xlabel('x');
ylabel('y');
title(' y = x.^3+x.^2+3*x+6');
plot(x,y,'p')
Problem 3
Generate a 100x50 matrix of random
numbers called A.
For each value inside the matrix, if the
value is above 0.5, change its value to 1.
If it is below 0.5, change its value to zero.
Then, count and display how many ones
inside the matrix.
Problem 4
Use the plot function to draw this figure:
Problem 5
Write a program that reads an image and
converts the image into its negative.
To convert to a negative image, for each pixel
value:
If the current pixel value is black (0), then change it to
white (255).
If the current pixel value is white (255), then change it
to black (0).
Show the original image and the changed
image.
White = 255, Black = 0
The End