0% found this document useful (0 votes)
26 views45 pages

Lecture 1 - Introduction To Matlab

Uploaded by

haashill
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views45 pages

Lecture 1 - Introduction To Matlab

Uploaded by

haashill
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 45

Introduction to Matlab

& Data Analysis

Lecture 1: Introduction

Lecture time:
Thursday 11:00 – 13:00

Course website: http://www.weizmann.ac.il/midrasha/courses/MatlabIntro/

Eran Eden, Weizmann 2008 © 1


Team members
 Lecturers:
 Natalie Kalev-Kronik [email protected]
 Eran Eden
 Maya Geva

 Tutors:
 Yuval Hart
 Maya Geva

 Exercise checkers:
 Yuval Dorfan
 Anat Tzimmer

2
Tips / formalities
 Course website
http://www.weizmann.ac.il/midrasha/courses/MatlabIntro

 The website contains


 Course material: Lectures + tutorials + other Matlab resources
 HW and solutions
 News

 Where can I do the HW?


 On any pc computer at Weizmann (installation of Matlab will be discussed in the
first tutorial)
 In the tutorial class

 Grade
 HWs 60% + 40% ( Exam)

 Course references
 Official course book: Mastering Matlab 7, Hanselman & Littlefield
 Matlab built in tutorial and references

3
Tips / formalities
 Signing up for tutorials

(#1) Wed 11:00 – 12:00


(#2) Wed 12:00 – 13:00
(#3) Wed 13:00 – 14:00

 HW assistance at the computer room

Once a week in Levine 101


Tuesday 8-9 am

4
Course overview
 Introduction to Matlab
Maya Geva:
 Matlab building blocks: 1D 2D and 3D  Image Processing toolbox
arrays

 Simple data analysis and graphics


 Signal processing tollbox, curvefitting toolbox
Eran Eden
 Control and boolean logic  The Bioinformatics Toolbox
 Unsupervised learning using Statistics
 Loops Toolbox
 Supervised learning & the Artificial Neural
 Functions and program design network Toolbox
 Cells, structures and Files

 Simple algorithms and complexity

 Debugger

 GUI toolbox

5
For whom is the course intended?

• For student with no or little experience of Matlab- first two thirds of


the course.
• For students familiar with Matlab who want to expand their knowledge
using Matlab toolboxes- especially last four weeks.

6
What is the course about?

(1) Programming in Matlab

(2) Tackling data analysis problems with Matlab

7
What is the course about?
Example #1 of a data analysis problem

CAGCATATTTGAAGCCGGGCCCACACACAATTGGGGAACGGATCCCCGCGGCCTCCCGGCA
GACCCCGTCCGGCACGACGACGAAGAAGGGGAGGATGAAGTCGAATTTGAAGCGGATGAAG
GATGAGGAGAGTGACGAAGAAGAGGACGAAGACGACGAGGTCCTTGACGAGGAAGTGAACT
ATTGAATTTGAAGCTTATTCCATCTCAGATAATGATTATGACGGAATTAAGAAATTACTAG
CAGCAGCTTTTCCTAAAGGCTCCTGTGAACACTGCAGAACTAACAGATCTCTTAATTCATA
CAGAACCATATTGGAAGTGTGAATTTGAAGCTTAAGCAAACAAATGTTTCAGAAGACAGCG
ATGATGATGATGCAGATGAAGATGAAATTTTTGGTTTCATAAGCCTTTTAAATTTAACTGA
AAGAAAGGTACCCAGTGTGCTGAACAAATTAAAGAGTTGGTATTTGAAGCGGGTGAGAAGA
ACTGTAAAGAATTTGAAGCGGCAGCTGGACAAGCTTTTAAATGACACCACCAAGCCTGTGG
GCTTTCTCCTAAGTGAAAGATTCATTAATGTCCCTCCTCAGATTGCTCTGCCCATGCACCA
GCAGCTTCAGAAAGAATTTGAAGCAATTTGAAGCCTAGTATTTGAAGCTTCTACCTTCTGA
GACCCCGTCCGGCACGACGACGAAGAAGGGGAGGATGAAGTCGAGGATGAAGACGAAGATC
GATGAGGAGAGTGACGAAGAAGAGGATTTGAAGCACGAAGACGACGAGGTCCTTGACGAGG
AAGTGAATATTGAATTTGAAGCTTATTCCATCTCAGATAATGATTATGACGGAATTAAGAA
ATTACTGCAGCAATTTGAAGCAAAGGCTCCTGTGAACACTGCAGATTTGAAGCAACTAACA
ATTCAACAGAACCATATTGGAAGTGTGATTAAGCAAACAAATGTTTCAGAAGACAGCGATG
ATGATGATGCATTTGAAGCAGATGAAGATGAAATTTTTGGTTTCATAAGCCTTTTAAATTT
CTAATAAGCCATGTGGGAAGTGCTCTTTCTACCTTATTTGAAGCACACCATTTGTGGAAGA
ATTACTGCAGCAATTTGAAGCAAAGGCTCCTGTGAACACTGCAGATTTGAAGCAACTAACA

8
What is the course about?
Example #1 of a data analysis problem

CAGCATATTTGAAGCCGGGCCCACACACAATTGGGGAACGGATCCCCGCGGCCTCCCGGCA
GACCCCGTCCGGCACGACGACGAAGAAGGGGAGGATGAAGTCGAATTTGAAGCGGATGAAG
GATGAGGAGAGTGACGAAGAAGAGGACGAAGACGACGAGGTCCTTGACGAGGAAGTGAACT
ATTGAATTTGAAGCTTATTCCATCTCAGATAATGATTATGACGGAATTAAGAAATTACTAG
CAGCAGCTTTTCCTAAAGGCTCCTGTGAACACTGCAGAACTAACAGATCTCTTAATTCATA
CAGAACCATATTGGAAGTGTGAATTTGAAGCTTAAGCAAACAAATGTTTCAGAAGACAGCG
ATGATGATGATGCAGATGAAGATGAAATTTTTGGTTTCATAAGCCTTTTAAATTTAACTGA
AAGAAAGGTACCCAGTGTGCTGAACAAATTAAAGAGTTGGTATTTGAAGCGGGTGAGAAGA
ACTGTAAAGAATTTGAAGCGGCAGCTGGACAAGCTTTTAAATGACACCACCAAGCCTGTGG
GCTTTCTCCTAAGTGAAAGATTCATTAATGTCCCTCCTCAGATTGCTCTGCCCATGCACCA
GCAGCTTCAGAAAGAATTTGAAGCAATTTGAAGCCTAGTATTTGAAGCTTCTACCTTCTGA
GACCCCGTCCGGCACGACGACGAAGAAGGGGAGGATGAAGTCGAGGATGAAGACGAAGATC
GATGAGGAGAGTGACGAAGAAGAGGATTTGAAGCACGAAGACGACGAGGTCCTTGACGAGG
AAGTGAATATTGAATTTGAAGCTTATTCCATCTCAGATAATGATTATGACGGAATTAAGAA
ATTACTGCAGCAATTTGAAGCAAAGGCTCCTGTGAACACTGCAGATTTGAAGCAACTAACA
ATTCAACAGAACCATATTGGAAGTGTGATTAAGCAAACAAATGTTTCAGAAGACAGCGATG
ATGATGATGCATTTGAAGCAGATGAAGATGAAATTTTTGGTTTCATAAGCCTTTTAAATTT
CTAATAAGCCATGTGGGAAGTGCTCTTTCTACCTTATTTGAAGCACACCATTTGTGGAAGA
ATTACTGCAGCAATTTGAAGCAAAGGCTCCTGTGAACACTGCAGATTTGAAGCAACTAACA

9
What is the course about?
Example #2 of a data analysis problem

10 21 10 21
73 21 18 21
10 4 8 21
3 21 10 45
8 21 2 21

10
What is the course about?
Examples #3-4 of data analysis problems

11
What is the course about?

(1) Programming in Matlab

(2) Tackling data analysis problems with Matlab

(3) Learn how to learn Matlab by yourself

12
Why Matlab?
 Easy to learn

 Easy to debug

 Great tool for scientific work


 Exploring your data
 Visualizing your data

 Many useful “toolboxes”

13
Matlab’s main disadvantage…

 It’s slower than other programming


languages.
 (unless you use the compiler)…

14
Background - computers
Output

Input
15
Background - hardware

CPU Memory

16
Background - software
High level languages
Examples: C, C++, C#, Java, Pascal, Perl, Lisp, Matlab

Low level language


Example: Assembly

Machine language
Example: 0111010101111101…
Another important player:
The operating system

17
The Matlab environment
First we need to Open Matlab

18
The Matlab environment
Opening/saving a file

Changing current directory


Prompt / Command line

Files and Directories


The command window
inside the current
directory

workspace

19
Matlab can be used as a calculator

20
Our first command
Writing a command in the command line

21
Our first script (M-file)
(1) Writing the script (2) Saving the script

Comments start
with a %
(3) Defining script name (4) Running the script

22
Making errors…

This command does NOT


exist in Matlab! Pressing here will bring you to
the line in the script where the
error occurred

23
Another script…
Making sophisticated graphics and animation in Matlab is easy.
We will learn how to do this in two lectures
Peaks

Z = peaks; surf(Z);
axis tight
set(gca,'nextplot','replacechildren');
5
% Record the movie
for j = 1:20
surf(sin(2*pi*j/20)*Z,Z) 0
F(j) = getframe;
end
-5
% Play the movie twenty times
movie(F,20) 2
3
2
0 1
0
-2 -1
-2
y -3
x

24
Help!!!
 help
 doc
 Example: doc disp
 Google

25
Matlab toolboxes

26
Introduction to Matlab
& Data Analysis

Topic #2:
The Matlab Building Blocks - Variables,
Arrays and Matrices

Eran Eden, Weizmann 2008 © 27


identifiers
 Identifiers are all the words that build up the program
 An identifier is a sequence of letters, digits and underscores “_”
 Maximal length of identifiers is 63 characters
 Can’t start with a digit
 Can’t be a reserved word

Examples of Legal Examples of illegal


identifiers: identifiers:

 time  007bond
 day_of_the_week  #time
 bond007  ba-baluba
 findWord  if
 while

28
An overview of the main players in
a program
Identifiers

Reserved Library Constants Variables User defined


words functions functions

29
Reserved words (keywords)
 Words that are part of the Matlab language
 There are 17 reserved words:

 for  if
 function
 elseif
 otherwise
 try
 continue
 break
 global
 end
 while
 return  case
 switch  else
 catch  persistent

 Do NOT try to redefine their meaning!

 Don NOT try to redefine their library function names either!

30
Constants
 The value of a constant is fixed and does not change
throughout the program

Numbers Chars
100 ‘c’
0.3
Strings
Arrays ‘I like to eat sushi’
[12345] ‘1 + 2’

Matrices
[5 3
4 2]
31
Variables
 Why do we need variables?
Computer memory

salary
9000

constant
new_salary
 Example:
27000
>> salary = 9000;
>> new_salary = salary * 3;
>> disp(new_salary);
variable
27000

Library functions
32
Variables
 Another example: The Matlab Console

price_bamba =
price_bamba = 3 3

What happens if you omit the ‘;’ ?

33
Variables
 Another example: The Matlab Console

price_bamba =
price_bamba = 3 3
n_bamba = 2;

What happens when we add the ‘;’ ?

34
Variables
The Matlab Console
 Another example:
price_bamba =
price_bamba = 3 3
n_bamba = 2; price_bisly =
price_bisly = 5 5
n_bisly = 3; total_price =
21

n_bamba =
5

total_price =
21

total_price = price_bamba * n_bamba + price_bisly * n_bisly


n_bamba = 5
total_price
35
Variables

 Tip #1: Give your variable meaningful names.


a = 9000
b = 100
are a bad choice for naming variables that store your working hours
and salary!

A more meaningful choice of names would


salary = 9000;
hours = 5;

36
Variables

 Tip #2: Don’t make variable names too long

salary_I_got_for_my_work_at_the_gasoline_station = 9000;

salary_I_got_for_my_work_in_the_bakery = salary_I_got_for_my_work_at_the_gasoline_station * 3;

disp(salary_I_got_for_my_work_in_the_bakery);

Very bad choice of variable name!!!

 When should I use capital letters ?


 Tip #3: Whatever you do - be consistent.

37
Variables Types
 Each variable has a type

 Why do we need variable types?

 Different types of variable store different types of data

>> a = 10
a = Returns the type
10 of a variable

>> class(a)
ans = The default variable type
double in Matlab is double
38
Variables Types
 Each variable has a type

 Why do we need variable types?

 Different types of variable store different types of data

>> a = 10 >> b = 10.56 >> c = 'Bush' >> d = true


a = b = c = d =
10 10.5600 Bush 1

>> class(a) >> class(b) >> class(c) >> class(d)


ans = ans = ans = ans =
double double char logical
39
Variables Types
 Different variable types require different memory allocations

>> a = 10.4 %double requires 8 bytes


a =
10.4
1 2 3 8
 How many bytes are required to store this variable: c = 'Bush' ?
1 0 0 0 1 1 0 0 1 0 1 1 1 0 0 0 0 0 0 0 1 0 0 0 … 1 0 0 0 1 0 0 0

>> b = 'B' %char requires 2 bytes


b =
B Memory allocation and
release is done
1 2 automatically in Matlab

1 0 0 0 1 1 0 0 1 0 1 1 1 0 0 0

40
Computer precision limitations
 How much is:
>> 0.42 + 0.08 - 0.5
ans =
0

 How much is:


>> 0.42 - 0.5 + 0.08
ans =
-1.3878e-017

41
Special variables
>> 4 * 5
 ans ans =
20

>> ans + 1
ans =
21

42
Special variables
>> 2 * inf
ans =
 ans Inf

 pi >> 1 / 0
Warning: Divide by zero.
 inf ans =
Inf

43
Special variables
>> 0 / 0
Warning: Divide by zero.
 ans ans =
NaN
 pi
 inf >> NaN + 1
ans =
 NaN NaN
 In the tutorial you’ll see more…

44
Summary
 Matlab is a high level language

 Matlab working environment

 Variables & variable types + how to use


them

45

You might also like