45
An Introduction to MATLAB Day 1 Simon Mitchell [email protected]

An Introduction to MATLAB - UCLA · Variables to result of expression If%an%expression%is%not%stored%as%a%variable%it%will%be%stores% as%‘ans’%

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: An Introduction to MATLAB - UCLA · Variables to result of expression If%an%expression%is%not%stored%as%a%variable%it%will%be%stores% as%‘ans’%

An Introduction to MATLABDay 1

Simon [email protected]

Page 2: An Introduction to MATLAB - UCLA · Variables to result of expression If%an%expression%is%not%stored%as%a%variable%it%will%be%stores% as%‘ans’%

�  High%level%language%�  Programing%language%and%development%environment%�  Built4in%development%tools%�  Numerical%manipulation%�  Plotting%of%functions%and%data%�  Implement%algorithms%�  Create%models%and%applications%�  Many%built%in%functions%�  Interface%with%other%languages%�  Create%graphical%interfaces%

Page 3: An Introduction to MATLAB - UCLA · Variables to result of expression If%an%expression%is%not%stored%as%a%variable%it%will%be%stores% as%‘ans’%

The MATLAB Environment

Page 4: An Introduction to MATLAB - UCLA · Variables to result of expression If%an%expression%is%not%stored%as%a%variable%it%will%be%stores% as%‘ans’%

Current Folder

Page 5: An Introduction to MATLAB - UCLA · Variables to result of expression If%an%expression%is%not%stored%as%a%variable%it%will%be%stores% as%‘ans’%

Command Window >>

Page 6: An Introduction to MATLAB - UCLA · Variables to result of expression If%an%expression%is%not%stored%as%a%variable%it%will%be%stores% as%‘ans’%

Workspace

Page 7: An Introduction to MATLAB - UCLA · Variables to result of expression If%an%expression%is%not%stored%as%a%variable%it%will%be%stores% as%‘ans’%

Editor

Page 8: An Introduction to MATLAB - UCLA · Variables to result of expression If%an%expression%is%not%stored%as%a%variable%it%will%be%stores% as%‘ans’%

Command Window Basics

Page 9: An Introduction to MATLAB - UCLA · Variables to result of expression If%an%expression%is%not%stored%as%a%variable%it%will%be%stores% as%‘ans’%

Common Arithmetic Operators

+ %Addition%4 %Subtraction%%* %Multiplication%/ %Division%^ %Exponential%() %Order%operations%%

Page 10: An Introduction to MATLAB - UCLA · Variables to result of expression If%an%expression%is%not%stored%as%a%variable%it%will%be%stores% as%‘ans’%

Semicolons in MATLAB

Suppress%the%output%from%a%MATLAB%expression%

Page 11: An Introduction to MATLAB - UCLA · Variables to result of expression If%an%expression%is%not%stored%as%a%variable%it%will%be%stores% as%‘ans’%

Comments %

Suppress%the%output%from%a%MATLAB%expression%

Page 12: An Introduction to MATLAB - UCLA · Variables to result of expression If%an%expression%is%not%stored%as%a%variable%it%will%be%stores% as%‘ans’%

Variables

Variable%name%=%variable%value%%%%%%%Be%careful%of%i,j%

Page 13: An Introduction to MATLAB - UCLA · Variables to result of expression If%an%expression%is%not%stored%as%a%variable%it%will%be%stores% as%‘ans’%

Variables 2

MATLAB%is%CASE%SENSITIVE%%%%%%%

Page 14: An Introduction to MATLAB - UCLA · Variables to result of expression If%an%expression%is%not%stored%as%a%variable%it%will%be%stores% as%‘ans’%

Close%MATLAB%%Reopen%MATLAB%

Page 15: An Introduction to MATLAB - UCLA · Variables to result of expression If%an%expression%is%not%stored%as%a%variable%it%will%be%stores% as%‘ans’%

Initially%Workspace%is%empty%

Page 16: An Introduction to MATLAB - UCLA · Variables to result of expression If%an%expression%is%not%stored%as%a%variable%it%will%be%stores% as%‘ans’%

Load%your%workspace%

Page 17: An Introduction to MATLAB - UCLA · Variables to result of expression If%an%expression%is%not%stored%as%a%variable%it%will%be%stores% as%‘ans’%

Variables to result of expression

If%an%expression%is%not%stored%as%a%variable%it%will%be%stores%as%‘ans’%

Page 18: An Introduction to MATLAB - UCLA · Variables to result of expression If%an%expression%is%not%stored%as%a%variable%it%will%be%stores% as%‘ans’%

Multiple assignments and ‘who’

Page 19: An Introduction to MATLAB - UCLA · Variables to result of expression If%an%expression%is%not%stored%as%a%variable%it%will%be%stores% as%‘ans’%

whos

Page 20: An Introduction to MATLAB - UCLA · Variables to result of expression If%an%expression%is%not%stored%as%a%variable%it%will%be%stores% as%‘ans’%

format

‘format%short’%%to%get%back%to%normal%

Page 21: An Introduction to MATLAB - UCLA · Variables to result of expression If%an%expression%is%not%stored%as%a%variable%it%will%be%stores% as%‘ans’%

Numbers are actually 1x1 Matrices

Page 22: An Introduction to MATLAB - UCLA · Variables to result of expression If%an%expression%is%not%stored%as%a%variable%it%will%be%stores% as%‘ans’%

Operations apply to matrices

Page 23: An Introduction to MATLAB - UCLA · Variables to result of expression If%an%expression%is%not%stored%as%a%variable%it%will%be%stores% as%‘ans’%

Operations apply to matrices

Page 24: An Introduction to MATLAB - UCLA · Variables to result of expression If%an%expression%is%not%stored%as%a%variable%it%will%be%stores% as%‘ans’%

Put . before an operator to make it

element-wise

disp(variable)%displays%the%contents%of%a%variable%

Page 25: An Introduction to MATLAB - UCLA · Variables to result of expression If%an%expression%is%not%stored%as%a%variable%it%will%be%stores% as%‘ans’%

fprintf – print something to command window

%s %Format%as%a%string.%%d %Format%as%an%integer.%%f %Format%as%a%floating%point%value.%%e %Format%as%a%floating%point%value%in%scientific%notation.%%g %Format%in%the%most%compact%form:%%f%or%%e.%\n %Insert%a%new%line%in%the%output%string.%\t %Insert%a%tab%in%the%output%string.%

Page 26: An Introduction to MATLAB - UCLA · Variables to result of expression If%an%expression%is%not%stored%as%a%variable%it%will%be%stores% as%‘ans’%

Concatenating matrices

Page 27: An Introduction to MATLAB - UCLA · Variables to result of expression If%an%expression%is%not%stored%as%a%variable%it%will%be%stores% as%‘ans’%

Concatenating matrices

Why%is%this%1,1?%

Page 28: An Introduction to MATLAB - UCLA · Variables to result of expression If%an%expression%is%not%stored%as%a%variable%it%will%be%stores% as%‘ans’%

Many Functions work on columns

Page 29: An Introduction to MATLAB - UCLA · Variables to result of expression If%an%expression%is%not%stored%as%a%variable%it%will%be%stores% as%‘ans’%

�  3%Ways%of%running%code%�  Command%Window%�  Scripts%�  Functions%

Working with m-files

m"files'

Page 30: An Introduction to MATLAB - UCLA · Variables to result of expression If%an%expression%is%not%stored%as%a%variable%it%will%be%stores% as%‘ans’%

New Script

Page 31: An Introduction to MATLAB - UCLA · Variables to result of expression If%an%expression%is%not%stored%as%a%variable%it%will%be%stores% as%‘ans’%

Our First Script

Page 32: An Introduction to MATLAB - UCLA · Variables to result of expression If%an%expression%is%not%stored%as%a%variable%it%will%be%stores% as%‘ans’%

Scripts can create variables in the workspace

Page 33: An Introduction to MATLAB - UCLA · Variables to result of expression If%an%expression%is%not%stored%as%a%variable%it%will%be%stores% as%‘ans’%

Data Types

�  Type%declarations%are%not%necessary%in%MATLAB%

�  MATLAB%automatically%decides%data%type%

Page 34: An Introduction to MATLAB - UCLA · Variables to result of expression If%an%expression%is%not%stored%as%a%variable%it%will%be%stores% as%‘ans’%

Data Types

single%4%single%precision%numerical%data%double %%4%double%precision%numerical%data%logical'"'logical%values%of%1%or%0,%represent%true%and%false%respectively%char''"'character%data%(strings%are%stored%as%vector%of%characters)%cell'array%4%array%of%indexed%cells,%each%capable%of%storing%an%array%of%a%different%dimension%and%data%type%structure%4%named%fields%capable%of%storing%an%array%of%a%different%dimension%and%data%type%function'handle%4%pointer%to%a%function%user'classes'"'objects%constructed%from%a%user4defined%class%Int8'uint8'int16'uint16'int32'uint32'int64'uint64'–%don’t%worry%about%these''

Page 35: An Introduction to MATLAB - UCLA · Variables to result of expression If%an%expression%is%not%stored%as%a%variable%it%will%be%stores% as%‘ans’%

Data Types

Page 36: An Introduction to MATLAB - UCLA · Variables to result of expression If%an%expression%is%not%stored%as%a%variable%it%will%be%stores% as%‘ans’%

Testing Data Types

Page 37: An Introduction to MATLAB - UCLA · Variables to result of expression If%an%expression%is%not%stored%as%a%variable%it%will%be%stores% as%‘ans’%

Relational Operators

< %Less%than%>= %Less%than%or%equal%to%> %Greater%than%>= %Greater%than%or%equal%to%== %Equal%to%~= %Not%equal%to%%%%%

Useful%for%if%statements!%

Page 38: An Introduction to MATLAB - UCLA · Variables to result of expression If%an%expression%is%not%stored%as%a%variable%it%will%be%stores% as%‘ans’%

If Statement

Never'run'

Page 39: An Introduction to MATLAB - UCLA · Variables to result of expression If%an%expression%is%not%stored%as%a%variable%it%will%be%stores% as%‘ans’%

If else Statement

Page 40: An Introduction to MATLAB - UCLA · Variables to result of expression If%an%expression%is%not%stored%as%a%variable%it%will%be%stores% as%‘ans’%

If elseif Statement

Page 41: An Introduction to MATLAB - UCLA · Variables to result of expression If%an%expression%is%not%stored%as%a%variable%it%will%be%stores% as%‘ans’%

Nested if Statement

Page 42: An Introduction to MATLAB - UCLA · Variables to result of expression If%an%expression%is%not%stored%as%a%variable%it%will%be%stores% as%‘ans’%

For loops

Page 43: An Introduction to MATLAB - UCLA · Variables to result of expression If%an%expression%is%not%stored%as%a%variable%it%will%be%stores% as%‘ans’%

Combining What We’ve Learned

Write%a%script'that%takes%a%list%of%gene%names%and%gene%expression%values%and%outputs%only%those%gene%names%over%a%threshold.%%PSUEDO%CODE:%For%each%gene%in%a%list:%

%if%its%expression%value%is%over%the%threshold%% %print%the%gene%name%and%the%expression%value%

Page 44: An Introduction to MATLAB - UCLA · Variables to result of expression If%an%expression%is%not%stored%as%a%variable%it%will%be%stores% as%‘ans’%

Combining What We’ve Learned

PSUEDO%CODE:%For%each%gene%in%a%list:%

%if%its%expression%value%is%over%the%threshold%% %print%the%gene%name%and%the%expression%value%

Page 45: An Introduction to MATLAB - UCLA · Variables to result of expression If%an%expression%is%not%stored%as%a%variable%it%will%be%stores% as%‘ans’%

Combining What We’ve Learned