SlideShare a Scribd company logo
The Basics of MATLAB
BY MUHAMMAD BILAL ALLI
Course Outline
❑Basic setup
❑Variables and arrays
❑For Loops
❑If Statements and Decision Making
❑User Input and Pausing
❑Saving and Loading Variables
❑Plotting
Basic Setup
Layout
Tabs : Home, Plots, Apps
The other important features are the command window and
Workspace
Variables and Arrays
Variables and Arrays
❑A variable is something that holds a number for us to carry out
computation with.
❑Variables can be a single number or a multitude of numbers called
an array or matrix.
Variables and Arrays
❑We can use the colon to generate a vector, which is series of
numbers, from a start point to an endpoint in steps of 1 or
increments we set.
❑Methods of addressing an individual element or number in an
array, an entire row or rows, column or columns are described in the
following examples.
Simple Variables
%% x,y and z are our variables.
x=1;
y=2;
z=x+y
%%
Matrices/Arrays
%% x, y and z are our variables , note that to declare a matrix
we use square brackets, a semicolon ends the row, since we have 3
entries we have 3 columns.
x=[1,1,1];
y=[2,2,2];
z=x+y
Matrices/Arrays
%% a, b and c are our variables, a semicolon ends the row, now we
have 3 rows.
a=[1;1;1;];
b=[2;2;2;];
c=a+b
Basic Colon Use
%% here we define an array x=[1,2,3] using the colon
x=[1:3]
Basic Colon Use
%% here we define an array x=[5,10,15] the number between the
colons is the step we increment by
y=[5:5:15]
Methods to Address Array Values
% We define our variable x below
x=[1,2,3;
4,5,6;
7,8,9;];
%% We setup a variable C to take the value 3 from the first row and third column
using the format new_variable=other_variable(Row_Number,Column_Number).
C=x(1,3)
% Answer C=3.
Methods to Address Array Values
% We define our variable x below
x=[1,2,3;
4,5,6;
7,8,9;];
%% Below we take the third row and assign its values to D, By using the colon in
the columns spot we have selected all columns in the third row.
D=x(3,:)
% Answer D = 7,8,9.
Methods to Address Array Values
% We define our variable x below
x=[1,2,3;
4,5,6;
7,8,9;];
%% We can do the opposite and select the third column and all rows.
E=x(:,3)
% Answer E = 3,6,9.
Methods to Address Array Values
% We define our variable x below
x=[1,2,3;
4,5,6;
7,8,9;];
%% Getting a little fancier we can select the last two rows and the first two
columns.
F=x(2:3,1:2);
% Answer F = [4,5;
% 7,8;];
For Loops
For Loops
❑In programming we find ourselves in situations where we need to
input or calculate a lot of data in a sequential or iterative fashion.
❑We use a variable called an index. The index changes its value on
each cycle of the loop. These cycles are referred to as iterations. The
lowercase i in the examples are the index of a loop, you may use any
letter or word you like to name your index.
For Loops to Address Arrays
%% We set a loop that runs ten times that calculates x added to the current index, by using
(i) next to y we can save each computed value as an addressable element in the array y.
x=10;
%% i increases in value from 1 to 10 in steps of 1,
% when i =1 y(i)=y(1) Which is the first element of the array y
% y(1)=x+1 This means we assign the value x+1 to the array element y(1).
for i=1:10
y(i)=x+i
end
If Statements and Decision Making
If Statements and Decision Making
❑When programming we often find ourselves in a position where we need to write code that
can make decisions. The simplest way to do this is by means of an If statement.
❑An If statement allows a piece of code to run if certain conditions are met. A few simple tricks
are conditions based on terms being greater than, less than or equal to certain values.
❑Else statements can be used in conjunction with If statements, if the If statements condition is
not met, you can set an else statement to run some code or provide subsequent If statements
beneath that Else statement to run code under certain conditions.
If Statements Using Greater Than
% Here we have a piece of code that iterates from 1 to 10, it counts the
number of values where i>3.
Numbeber_of_values_over_3=0;
for i=1:10
if i>3
Numbeber_of_values_over_3=Numbeber_of_values_over_3+1;
end
end
%% Answer Numbeber_of_values_over_3 = 7.
If Statements Using Less Than
% Here we have a piece of code that iterates from 1 to 10, it counts the number
of values where i<5.
Numbeber_of_values_under_5=0;
for i=1:10
if i<5
Numbeber_of_values_under_5=Numbeber_of_values_under_5+1;
end
end
%% Numbeber_of_values_under_5 = 4.
If Statements Using AND
% Here we have a piece of code that iterates from 1 to 10, it counts the
number of values where i<9 AND i>6, thereby counting the numbers between 9 and 6.
Numbeber_of_values_under_9_but_over_4=0;
for i=1:10
if i<9 && i>6
Numbeber_of_values_under_9_but_over_4=Numbeber_of_values_under_9_but_over_4+1;
end
end
%% Numbeber_of_values_under_9_but_over_4 = 2.
Else Statements and Using Equals To
x=[1,1,2,2,2];
Number_of_ones=0;
Number_of_twos=0;
for i=1:5
if x(i)==1
Number_of_ones=Number_of_ones+1;
else
Number_of_twos=Number_of_twos+1;
end
end
%% Answer Number_of_ones = 2; Number_of_twos = 3;
Using Multiple If and Else Statements
x=[1,1,2,2,3];
Number_of_ones=0;
Number_of_twos=0;
Number_of_threes=0;
for i=1:5
if x(i)==1
Number_of_ones=Number_of_ones+1;
else
if x(i)==2
Number_of_twos=Number_of_twos+1;
else
if x(i)==3
Number_of_threes=Number_of_threes+1;
end
end
end
end
%% Answer Number_of_ones = 2 ; Number_of_twos = 2; Number_of_threes=1;
User Input and Pausing
User Input and Pausing
❑In programming you may want to prompt user input as you run
your code instead of predefining everything.
❑Sometimes we may want to view things in a controlled fashion. By
using pause we can view changes on each iteration and see how they
develop things.
User Input
%% Here we establish a loop that runs for 1000 intervals, if the user inputs 1 they escape
the loop, i prints out each time denoting the interval number.
for i=1:1000
N = 'Would you like to end this loop? Enter 1 ';
N = input(N)
i
if N== 1
break
end
end
%%
Pausing
%% Here we establish a loop that runs for 100 intervals, on each interval the
loop prints a value of x and is then paused, press the spacebar to continue.
for i=1:100
x=i^2
pause
end
%% A useful trick to escape a long loop is to press control and C together in the
command window.
Saving and Loading Variables
Saving and Loading Variables
❑Once you have completed some calculations, you may want to save
your variables to a file and read them in later rather than carrying
out the calculations all over again the next time you boot up your
computer.
❑MATLAB® can save and load variables in a variety of formats, an
example of how to do that with text files is shown in the following
example.
Saving and Loading a Variable
%% Here we establish the Array a.
a=[1:1:5;
2:2:10;];
%% Here we save the Array a as a text file
dlmwrite('filename.txt',a,'delimiter',' ');
%% Here we load the values from that text file into a variable M.
M = dlmread('filename.txt')
%%
Plotting
Plotting
❑Plotting is the act of drawing a figure with your data, it helps you
visualize the numbers you are working with.
❑There are several nuances with plotting that we will go through
step by step.
Basic Plot of a Line
%% Below we establish the variable a and then plot it
a=[1,2,3,4,0,6,7,8,12,-3];
plot(a)
%%Note that plot as well as other functions are case sensitive,
if
%%you spelt Plot instead of plot you would get an error message
Basic Plot of a Line
Basic Plot of Data Points
%% Below we establish the variable a and then plot the data
points
a=[1,2,3,4,0,6,7,8,12,-3];
plot(a,’*’)
Basic Plot of Data Points
Notes
❑You will find yourself if situations where you will have x and y data that you want to plot to get
a line with some meaning.
❑One caveat to this is that the arrays holding x and y data must have the same number of
entries, or you will get an error message.
❑Next, we’ll look at examples that plots a few variables, it will cover labelling axis, adding a
legend, changing font size and setting the color for our lines.
Plotting Many Variables at Once
x=[1:10];
car1=[1,2,3,4,5,6,7,8,9,10];
car2=[3,3,3,3,3,3,3,3,3,3];
car3=[10,9,8,7,6,5,4,3,2,1];
plot(x,car1,'k',x,car2,'b',x,car3, 'r')
legend('Car 1','Car 2', 'Car 3')
xlabel('Time','FontSize',28)
ylabel('Speed','FontSize',28)
title('My First Title','FontSize',28)
Plotting Many Variables at Once
Plotting Many Variables at Once
x=[1:10];
car1=[1,2,3,4,5,6,7,8,9,10];
car2=[3,3,3,3,3,3,3,3,3,3];
car3=[10,9,8,7,6,5,4,3,2,1];
plot(x,car1,'k*',x,car2,'b*',x,car3, 'r*')
legend('Car 1','Car 2', 'Car 3')
xlabel('Time','FontSize',28)
ylabel('Speed','FontSize',28)
title('My First Title','FontSize',28)
Plotting Many Variables at Once
End of Class
❑Thank you for watching

More Related Content

What's hot

Algorithms and flowcharts1
Algorithms and flowcharts1Algorithms and flowcharts1
Algorithms and flowcharts1Lincoln School
 
WEKA: Practical Machine Learning Tools And Techniques
WEKA: Practical Machine Learning Tools And TechniquesWEKA: Practical Machine Learning Tools And Techniques
WEKA: Practical Machine Learning Tools And Techniques
DataminingTools Inc
 
Project in TLE
Project in TLEProject in TLE
Project in TLE
PGT_13
 
Machine learning session6(decision trees random forrest)
Machine learning   session6(decision trees random forrest)Machine learning   session6(decision trees random forrest)
Machine learning session6(decision trees random forrest)
Abhimanyu Dwivedi
 
Machine learning session7(nb classifier k-nn)
Machine learning   session7(nb classifier k-nn)Machine learning   session7(nb classifier k-nn)
Machine learning session7(nb classifier k-nn)
Abhimanyu Dwivedi
 
Report Group 4 Constants and Variables
Report Group 4 Constants and VariablesReport Group 4 Constants and Variables
Report Group 4 Constants and Variables
Genard Briane Ancero
 
FLOW OF CONTROL-NESTED IFS IN PYTHON
FLOW OF CONTROL-NESTED IFS IN PYTHONFLOW OF CONTROL-NESTED IFS IN PYTHON
FLOW OF CONTROL-NESTED IFS IN PYTHON
vikram mahendra
 
Chapter 2 : Balagurusamy_ Programming ANsI in C
Chapter 2 :  Balagurusamy_ Programming ANsI in CChapter 2 :  Balagurusamy_ Programming ANsI in C
Chapter 2 : Balagurusamy_ Programming ANsI in C
BUBT
 
Machine learning session8(svm nlp)
Machine learning   session8(svm nlp)Machine learning   session8(svm nlp)
Machine learning session8(svm nlp)
Abhimanyu Dwivedi
 
INTRODUCTION TO MATLAB session with notes
  INTRODUCTION TO MATLAB   session with  notes  INTRODUCTION TO MATLAB   session with  notes
INTRODUCTION TO MATLAB session with notes
Infinity Tech Solutions
 
Flowchart and algorithm
Flowchart and algorithmFlowchart and algorithm
Flowchart and algorithm
Sayali Shivarkar
 
358 33 powerpoint-slides_5-arrays_chapter-5
358 33 powerpoint-slides_5-arrays_chapter-5358 33 powerpoint-slides_5-arrays_chapter-5
358 33 powerpoint-slides_5-arrays_chapter-5
sumitbardhan
 
Chapter 3 : Balagurusamy Programming ANSI in C
Chapter 3 : Balagurusamy Programming ANSI in C Chapter 3 : Balagurusamy Programming ANSI in C
Chapter 3 : Balagurusamy Programming ANSI in C
BUBT
 
VISUAL BASIC 6 - CONTROLS AND DECLARATIONS
VISUAL BASIC 6 - CONTROLS AND DECLARATIONSVISUAL BASIC 6 - CONTROLS AND DECLARATIONS
VISUAL BASIC 6 - CONTROLS AND DECLARATIONS
Suraj Kumar
 
Structure & union
Structure & unionStructure & union
Structure & union
lalithambiga kamaraj
 
1153 algorithms%20and%20flowcharts
1153 algorithms%20and%20flowcharts1153 algorithms%20and%20flowcharts
1153 algorithms%20and%20flowchartsDani Garnida
 
Cs1123 5 selection_if
Cs1123 5 selection_ifCs1123 5 selection_if
Cs1123 5 selection_ifTAlha MAlik
 

What's hot (19)

Algorithms and flowcharts1
Algorithms and flowcharts1Algorithms and flowcharts1
Algorithms and flowcharts1
 
WEKA: Practical Machine Learning Tools And Techniques
WEKA: Practical Machine Learning Tools And TechniquesWEKA: Practical Machine Learning Tools And Techniques
WEKA: Practical Machine Learning Tools And Techniques
 
C code examples
C code examplesC code examples
C code examples
 
Project in TLE
Project in TLEProject in TLE
Project in TLE
 
Machine learning session6(decision trees random forrest)
Machine learning   session6(decision trees random forrest)Machine learning   session6(decision trees random forrest)
Machine learning session6(decision trees random forrest)
 
Machine learning session7(nb classifier k-nn)
Machine learning   session7(nb classifier k-nn)Machine learning   session7(nb classifier k-nn)
Machine learning session7(nb classifier k-nn)
 
Report Group 4 Constants and Variables
Report Group 4 Constants and VariablesReport Group 4 Constants and Variables
Report Group 4 Constants and Variables
 
FLOW OF CONTROL-NESTED IFS IN PYTHON
FLOW OF CONTROL-NESTED IFS IN PYTHONFLOW OF CONTROL-NESTED IFS IN PYTHON
FLOW OF CONTROL-NESTED IFS IN PYTHON
 
Chapter 2 : Balagurusamy_ Programming ANsI in C
Chapter 2 :  Balagurusamy_ Programming ANsI in CChapter 2 :  Balagurusamy_ Programming ANsI in C
Chapter 2 : Balagurusamy_ Programming ANsI in C
 
Machine learning session8(svm nlp)
Machine learning   session8(svm nlp)Machine learning   session8(svm nlp)
Machine learning session8(svm nlp)
 
INTRODUCTION TO MATLAB session with notes
  INTRODUCTION TO MATLAB   session with  notes  INTRODUCTION TO MATLAB   session with  notes
INTRODUCTION TO MATLAB session with notes
 
Flowchart and algorithm
Flowchart and algorithmFlowchart and algorithm
Flowchart and algorithm
 
358 33 powerpoint-slides_5-arrays_chapter-5
358 33 powerpoint-slides_5-arrays_chapter-5358 33 powerpoint-slides_5-arrays_chapter-5
358 33 powerpoint-slides_5-arrays_chapter-5
 
Flow chart
Flow chartFlow chart
Flow chart
 
Chapter 3 : Balagurusamy Programming ANSI in C
Chapter 3 : Balagurusamy Programming ANSI in C Chapter 3 : Balagurusamy Programming ANSI in C
Chapter 3 : Balagurusamy Programming ANSI in C
 
VISUAL BASIC 6 - CONTROLS AND DECLARATIONS
VISUAL BASIC 6 - CONTROLS AND DECLARATIONSVISUAL BASIC 6 - CONTROLS AND DECLARATIONS
VISUAL BASIC 6 - CONTROLS AND DECLARATIONS
 
Structure & union
Structure & unionStructure & union
Structure & union
 
1153 algorithms%20and%20flowcharts
1153 algorithms%20and%20flowcharts1153 algorithms%20and%20flowcharts
1153 algorithms%20and%20flowcharts
 
Cs1123 5 selection_if
Cs1123 5 selection_ifCs1123 5 selection_if
Cs1123 5 selection_if
 

Similar to The Basics of MATLAB

Introduction to C programming language. Coding
Introduction to C programming language. CodingIntroduction to C programming language. Coding
Introduction to C programming language. Coding
TanishqGosavi
 
CHAPTER 5
CHAPTER 5CHAPTER 5
CHAPTER 5
mohd_mizan
 
COM1407: Arrays
COM1407: ArraysCOM1407: Arrays
COM1407: Arrays
Hemantha Kulathilake
 
Introduction to Basic C programming 01
Introduction to Basic C programming 01Introduction to Basic C programming 01
Introduction to Basic C programming 01
Wingston
 
vb.net.pdf
vb.net.pdfvb.net.pdf
vb.net.pdf
VimalSangar1
 
[ITP - Lecture 15] Arrays & its Types
[ITP - Lecture 15] Arrays & its Types[ITP - Lecture 15] Arrays & its Types
[ITP - Lecture 15] Arrays & its Types
Muhammad Hammad Waseem
 
Lecture 01 - Introduction and Review.ppt
Lecture 01 - Introduction and Review.pptLecture 01 - Introduction and Review.ppt
Lecture 01 - Introduction and Review.ppt
MaiGaafar
 
Tutorial 2
Tutorial     2Tutorial     2
Tutorial 2
Mohamed Yaser
 
comp 122 Chapter 2.pptx,language semantics
comp 122 Chapter 2.pptx,language semanticscomp 122 Chapter 2.pptx,language semantics
comp 122 Chapter 2.pptx,language semantics
floraaluoch3
 
presentation.pptx
presentation.pptxpresentation.pptx
presentation.pptx
raghav415187
 
Array-part1
Array-part1Array-part1
Array-part1
AbishaiAsir
 
Arrays-Computer programming
Arrays-Computer programmingArrays-Computer programming
Arrays-Computer programming
nmahi96
 
Chapter1.pptx
Chapter1.pptxChapter1.pptx
Chapter1.pptx
WondimuBantihun1
 
L06
L06L06
R for Statistical Computing
R for Statistical ComputingR for Statistical Computing
R for Statistical Computing
Mohammed El Rafie Tarabay
 
Machine learning session4(linear regression)
Machine learning   session4(linear regression)Machine learning   session4(linear regression)
Machine learning session4(linear regression)
Abhimanyu Dwivedi
 
Explore ml day 2
Explore ml day 2Explore ml day 2
Explore ml day 2
preetikumara
 
java basics - keywords, statements data types and arrays
java basics - keywords, statements data types and arraysjava basics - keywords, statements data types and arrays
java basics - keywords, statements data types and arrays
mellosuji
 
CP Handout#7
CP Handout#7CP Handout#7
CP Handout#7
trupti1976
 

Similar to The Basics of MATLAB (20)

Introduction to C programming language. Coding
Introduction to C programming language. CodingIntroduction to C programming language. Coding
Introduction to C programming language. Coding
 
CHAPTER 5
CHAPTER 5CHAPTER 5
CHAPTER 5
 
COM1407: Arrays
COM1407: ArraysCOM1407: Arrays
COM1407: Arrays
 
Introduction to Basic C programming 01
Introduction to Basic C programming 01Introduction to Basic C programming 01
Introduction to Basic C programming 01
 
vb.net.pdf
vb.net.pdfvb.net.pdf
vb.net.pdf
 
[ITP - Lecture 15] Arrays & its Types
[ITP - Lecture 15] Arrays & its Types[ITP - Lecture 15] Arrays & its Types
[ITP - Lecture 15] Arrays & its Types
 
Lecture 01 - Introduction and Review.ppt
Lecture 01 - Introduction and Review.pptLecture 01 - Introduction and Review.ppt
Lecture 01 - Introduction and Review.ppt
 
Tutorial 2
Tutorial     2Tutorial     2
Tutorial 2
 
comp 122 Chapter 2.pptx,language semantics
comp 122 Chapter 2.pptx,language semanticscomp 122 Chapter 2.pptx,language semantics
comp 122 Chapter 2.pptx,language semantics
 
presentation.pptx
presentation.pptxpresentation.pptx
presentation.pptx
 
Array-part1
Array-part1Array-part1
Array-part1
 
Arrays-Computer programming
Arrays-Computer programmingArrays-Computer programming
Arrays-Computer programming
 
Chapter1.pptx
Chapter1.pptxChapter1.pptx
Chapter1.pptx
 
L06
L06L06
L06
 
R for Statistical Computing
R for Statistical ComputingR for Statistical Computing
R for Statistical Computing
 
Machine learning session4(linear regression)
Machine learning   session4(linear regression)Machine learning   session4(linear regression)
Machine learning session4(linear regression)
 
Explore ml day 2
Explore ml day 2Explore ml day 2
Explore ml day 2
 
java basics - keywords, statements data types and arrays
java basics - keywords, statements data types and arraysjava basics - keywords, statements data types and arrays
java basics - keywords, statements data types and arrays
 
Ch08
Ch08Ch08
Ch08
 
CP Handout#7
CP Handout#7CP Handout#7
CP Handout#7
 

Recently uploaded

Q1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year ReboundQ1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year Rebound
Oppotus
 
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
ewymefz
 
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
NABLAS株式会社
 
Empowering Data Analytics Ecosystem.pptx
Empowering Data Analytics Ecosystem.pptxEmpowering Data Analytics Ecosystem.pptx
Empowering Data Analytics Ecosystem.pptx
benishzehra469
 
Sample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdf
Sample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdfSample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdf
Sample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdf
Linda486226
 
一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单
ocavb
 
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
ukgaet
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP
 
Tabula.io Cheatsheet: automate your data workflows
Tabula.io Cheatsheet: automate your data workflowsTabula.io Cheatsheet: automate your data workflows
Tabula.io Cheatsheet: automate your data workflows
alex933524
 
Business update Q1 2024 Lar España Real Estate SOCIMI
Business update Q1 2024 Lar España Real Estate SOCIMIBusiness update Q1 2024 Lar España Real Estate SOCIMI
Business update Q1 2024 Lar España Real Estate SOCIMI
AlejandraGmez176757
 
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
nscud
 
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project PresentationPredicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Boston Institute of Analytics
 
FP Growth Algorithm and its Applications
FP Growth Algorithm and its ApplicationsFP Growth Algorithm and its Applications
FP Growth Algorithm and its Applications
MaleehaSheikh2
 
The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...
jerlynmaetalle
 
SOCRadar Germany 2024 Threat Landscape Report
SOCRadar Germany 2024 Threat Landscape ReportSOCRadar Germany 2024 Threat Landscape Report
SOCRadar Germany 2024 Threat Landscape Report
SOCRadar
 
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
nscud
 
Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)
TravisMalana
 
Innovative Methods in Media and Communication Research by Sebastian Kubitschk...
Innovative Methods in Media and Communication Research by Sebastian Kubitschk...Innovative Methods in Media and Communication Research by Sebastian Kubitschk...
Innovative Methods in Media and Communication Research by Sebastian Kubitschk...
correoyaya
 
Adjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTESAdjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTES
Subhajit Sahu
 
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Subhajit Sahu
 

Recently uploaded (20)

Q1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year ReboundQ1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year Rebound
 
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
 
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
 
Empowering Data Analytics Ecosystem.pptx
Empowering Data Analytics Ecosystem.pptxEmpowering Data Analytics Ecosystem.pptx
Empowering Data Analytics Ecosystem.pptx
 
Sample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdf
Sample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdfSample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdf
Sample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdf
 
一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单
 
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
 
Tabula.io Cheatsheet: automate your data workflows
Tabula.io Cheatsheet: automate your data workflowsTabula.io Cheatsheet: automate your data workflows
Tabula.io Cheatsheet: automate your data workflows
 
Business update Q1 2024 Lar España Real Estate SOCIMI
Business update Q1 2024 Lar España Real Estate SOCIMIBusiness update Q1 2024 Lar España Real Estate SOCIMI
Business update Q1 2024 Lar España Real Estate SOCIMI
 
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
 
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project PresentationPredicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
 
FP Growth Algorithm and its Applications
FP Growth Algorithm and its ApplicationsFP Growth Algorithm and its Applications
FP Growth Algorithm and its Applications
 
The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...
 
SOCRadar Germany 2024 Threat Landscape Report
SOCRadar Germany 2024 Threat Landscape ReportSOCRadar Germany 2024 Threat Landscape Report
SOCRadar Germany 2024 Threat Landscape Report
 
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
 
Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)
 
Innovative Methods in Media and Communication Research by Sebastian Kubitschk...
Innovative Methods in Media and Communication Research by Sebastian Kubitschk...Innovative Methods in Media and Communication Research by Sebastian Kubitschk...
Innovative Methods in Media and Communication Research by Sebastian Kubitschk...
 
Adjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTESAdjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTES
 
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
 

The Basics of MATLAB

  • 1. The Basics of MATLAB BY MUHAMMAD BILAL ALLI
  • 2. Course Outline ❑Basic setup ❑Variables and arrays ❑For Loops ❑If Statements and Decision Making ❑User Input and Pausing ❑Saving and Loading Variables ❑Plotting
  • 4. Layout Tabs : Home, Plots, Apps The other important features are the command window and Workspace
  • 6. Variables and Arrays ❑A variable is something that holds a number for us to carry out computation with. ❑Variables can be a single number or a multitude of numbers called an array or matrix.
  • 7. Variables and Arrays ❑We can use the colon to generate a vector, which is series of numbers, from a start point to an endpoint in steps of 1 or increments we set. ❑Methods of addressing an individual element or number in an array, an entire row or rows, column or columns are described in the following examples.
  • 8. Simple Variables %% x,y and z are our variables. x=1; y=2; z=x+y %%
  • 9. Matrices/Arrays %% x, y and z are our variables , note that to declare a matrix we use square brackets, a semicolon ends the row, since we have 3 entries we have 3 columns. x=[1,1,1]; y=[2,2,2]; z=x+y
  • 10. Matrices/Arrays %% a, b and c are our variables, a semicolon ends the row, now we have 3 rows. a=[1;1;1;]; b=[2;2;2;]; c=a+b
  • 11. Basic Colon Use %% here we define an array x=[1,2,3] using the colon x=[1:3]
  • 12. Basic Colon Use %% here we define an array x=[5,10,15] the number between the colons is the step we increment by y=[5:5:15]
  • 13. Methods to Address Array Values % We define our variable x below x=[1,2,3; 4,5,6; 7,8,9;]; %% We setup a variable C to take the value 3 from the first row and third column using the format new_variable=other_variable(Row_Number,Column_Number). C=x(1,3) % Answer C=3.
  • 14. Methods to Address Array Values % We define our variable x below x=[1,2,3; 4,5,6; 7,8,9;]; %% Below we take the third row and assign its values to D, By using the colon in the columns spot we have selected all columns in the third row. D=x(3,:) % Answer D = 7,8,9.
  • 15. Methods to Address Array Values % We define our variable x below x=[1,2,3; 4,5,6; 7,8,9;]; %% We can do the opposite and select the third column and all rows. E=x(:,3) % Answer E = 3,6,9.
  • 16. Methods to Address Array Values % We define our variable x below x=[1,2,3; 4,5,6; 7,8,9;]; %% Getting a little fancier we can select the last two rows and the first two columns. F=x(2:3,1:2); % Answer F = [4,5; % 7,8;];
  • 18. For Loops ❑In programming we find ourselves in situations where we need to input or calculate a lot of data in a sequential or iterative fashion. ❑We use a variable called an index. The index changes its value on each cycle of the loop. These cycles are referred to as iterations. The lowercase i in the examples are the index of a loop, you may use any letter or word you like to name your index.
  • 19. For Loops to Address Arrays %% We set a loop that runs ten times that calculates x added to the current index, by using (i) next to y we can save each computed value as an addressable element in the array y. x=10; %% i increases in value from 1 to 10 in steps of 1, % when i =1 y(i)=y(1) Which is the first element of the array y % y(1)=x+1 This means we assign the value x+1 to the array element y(1). for i=1:10 y(i)=x+i end
  • 20. If Statements and Decision Making
  • 21. If Statements and Decision Making ❑When programming we often find ourselves in a position where we need to write code that can make decisions. The simplest way to do this is by means of an If statement. ❑An If statement allows a piece of code to run if certain conditions are met. A few simple tricks are conditions based on terms being greater than, less than or equal to certain values. ❑Else statements can be used in conjunction with If statements, if the If statements condition is not met, you can set an else statement to run some code or provide subsequent If statements beneath that Else statement to run code under certain conditions.
  • 22. If Statements Using Greater Than % Here we have a piece of code that iterates from 1 to 10, it counts the number of values where i>3. Numbeber_of_values_over_3=0; for i=1:10 if i>3 Numbeber_of_values_over_3=Numbeber_of_values_over_3+1; end end %% Answer Numbeber_of_values_over_3 = 7.
  • 23. If Statements Using Less Than % Here we have a piece of code that iterates from 1 to 10, it counts the number of values where i<5. Numbeber_of_values_under_5=0; for i=1:10 if i<5 Numbeber_of_values_under_5=Numbeber_of_values_under_5+1; end end %% Numbeber_of_values_under_5 = 4.
  • 24. If Statements Using AND % Here we have a piece of code that iterates from 1 to 10, it counts the number of values where i<9 AND i>6, thereby counting the numbers between 9 and 6. Numbeber_of_values_under_9_but_over_4=0; for i=1:10 if i<9 && i>6 Numbeber_of_values_under_9_but_over_4=Numbeber_of_values_under_9_but_over_4+1; end end %% Numbeber_of_values_under_9_but_over_4 = 2.
  • 25. Else Statements and Using Equals To x=[1,1,2,2,2]; Number_of_ones=0; Number_of_twos=0; for i=1:5 if x(i)==1 Number_of_ones=Number_of_ones+1; else Number_of_twos=Number_of_twos+1; end end %% Answer Number_of_ones = 2; Number_of_twos = 3;
  • 26. Using Multiple If and Else Statements x=[1,1,2,2,3]; Number_of_ones=0; Number_of_twos=0; Number_of_threes=0; for i=1:5 if x(i)==1 Number_of_ones=Number_of_ones+1; else if x(i)==2 Number_of_twos=Number_of_twos+1; else if x(i)==3 Number_of_threes=Number_of_threes+1; end end end end %% Answer Number_of_ones = 2 ; Number_of_twos = 2; Number_of_threes=1;
  • 27. User Input and Pausing
  • 28. User Input and Pausing ❑In programming you may want to prompt user input as you run your code instead of predefining everything. ❑Sometimes we may want to view things in a controlled fashion. By using pause we can view changes on each iteration and see how they develop things.
  • 29. User Input %% Here we establish a loop that runs for 1000 intervals, if the user inputs 1 they escape the loop, i prints out each time denoting the interval number. for i=1:1000 N = 'Would you like to end this loop? Enter 1 '; N = input(N) i if N== 1 break end end %%
  • 30. Pausing %% Here we establish a loop that runs for 100 intervals, on each interval the loop prints a value of x and is then paused, press the spacebar to continue. for i=1:100 x=i^2 pause end %% A useful trick to escape a long loop is to press control and C together in the command window.
  • 31. Saving and Loading Variables
  • 32. Saving and Loading Variables ❑Once you have completed some calculations, you may want to save your variables to a file and read them in later rather than carrying out the calculations all over again the next time you boot up your computer. ❑MATLAB® can save and load variables in a variety of formats, an example of how to do that with text files is shown in the following example.
  • 33. Saving and Loading a Variable %% Here we establish the Array a. a=[1:1:5; 2:2:10;]; %% Here we save the Array a as a text file dlmwrite('filename.txt',a,'delimiter',' '); %% Here we load the values from that text file into a variable M. M = dlmread('filename.txt') %%
  • 35. Plotting ❑Plotting is the act of drawing a figure with your data, it helps you visualize the numbers you are working with. ❑There are several nuances with plotting that we will go through step by step.
  • 36. Basic Plot of a Line %% Below we establish the variable a and then plot it a=[1,2,3,4,0,6,7,8,12,-3]; plot(a) %%Note that plot as well as other functions are case sensitive, if %%you spelt Plot instead of plot you would get an error message
  • 37. Basic Plot of a Line
  • 38. Basic Plot of Data Points %% Below we establish the variable a and then plot the data points a=[1,2,3,4,0,6,7,8,12,-3]; plot(a,’*’)
  • 39. Basic Plot of Data Points
  • 40. Notes ❑You will find yourself if situations where you will have x and y data that you want to plot to get a line with some meaning. ❑One caveat to this is that the arrays holding x and y data must have the same number of entries, or you will get an error message. ❑Next, we’ll look at examples that plots a few variables, it will cover labelling axis, adding a legend, changing font size and setting the color for our lines.
  • 41. Plotting Many Variables at Once x=[1:10]; car1=[1,2,3,4,5,6,7,8,9,10]; car2=[3,3,3,3,3,3,3,3,3,3]; car3=[10,9,8,7,6,5,4,3,2,1]; plot(x,car1,'k',x,car2,'b',x,car3, 'r') legend('Car 1','Car 2', 'Car 3') xlabel('Time','FontSize',28) ylabel('Speed','FontSize',28) title('My First Title','FontSize',28)
  • 43. Plotting Many Variables at Once x=[1:10]; car1=[1,2,3,4,5,6,7,8,9,10]; car2=[3,3,3,3,3,3,3,3,3,3]; car3=[10,9,8,7,6,5,4,3,2,1]; plot(x,car1,'k*',x,car2,'b*',x,car3, 'r*') legend('Car 1','Car 2', 'Car 3') xlabel('Time','FontSize',28) ylabel('Speed','FontSize',28) title('My First Title','FontSize',28)
  • 45. End of Class ❑Thank you for watching