SlideShare une entreprise Scribd logo
1  sur  32
Télécharger pour lire hors ligne
MATLAB
Matalb
• =>
• =>
• => i.e. integer -> double
•
•
toolbox
toolbox
toolbox
Matlab
Matlab
current
folder
variablecommand line
command
• lookfor <keyword>
• help <command>
• a=1
• a=2;
• whos a
• clear 0
• %
simple calculator
• a = 1
• b = a+1
• c = a + b
• d = a*b^c - c/b
predefined variables
• pi %=> 3.14……
• i or j %=> 0.0000+1.0000i
• %Tips: use 1i
variable declaration
• s => undefined
• scalar: s=1
• vector:
• v1=[1,2,3]
• v1=1:3
• v2=[1;2;3]
• v2=[1,2,3]'
• matrix
• m1=[1,2;3,4]
1 2
3 4
row 1
row 2
col 1 col 2
vector indexing
• v=1:2:10
• v(5)
• v(1:3)
• v(:)
• v(end)
• v(1:end~=3)
• Try it:
• Get the 2nd element
from v.
• Get 3rd to end
elements.
• Put 1st, 3rd, 5th
element as a new
array named b.
matrix indexing
• matrix
• m=[1:3;4:6;7:9]
1 2 3
4 5 6
7 8 9
• matrix
• m=[1:3;4:6;7:9]
• m(1,:) % get 1st row
• m(:) % get all
element
• m(1:2,3) % submatrix
• m(8) ???
• linear indexing
• m([1,4,5,6])
try it
• m = magic(5)
• Get first column from m.
• Get 3~5 rows from m.
• Get submatrix which formed by the intersection of 1~3
rows and 2~4 columns of m and store in a new matrix
m1(3x3 matrix).
• Get a column vector which formed by all the odd linear
index elements in m.
operator
• matrix operatior
• *
• /
• ^
• …..
• element-wise operator
• .*
• ./
• .^
• …..
try it
• v1=1:3
• v2=v1'
• v3=v1*2
• v3*v2
• v2*v3
• v1.*v3
• v1.^v3
• m1=[1:3;4:6]
• m2=m1'
• m3=m1*2
• m3*m2
• m2*m3
• m1.*m3
• 10.^m1
matlab function
• v=1:20;
• sin(v)
• cos(v), tan(v)….
• exp(v), log(v)…
• diag, eig( )
• sum( )
• check how to use: help <function>
try it
• m = magic(100);
• Get following arrays:
• 1st row
• 25th column
• diagonal
• Get the summation of above arrays. Are they the
same?
example 1:
eigenvalue problem
• A=magic(5)
• [vectors, values] = eig(A)
• isequal(A*vectors,vectors*values) %=> ?
• A*vectors-vectors*values %=> ?
example 2:
make a plot
• linspace(<start>,<end>,<sep>)
• x = linspace(0,2*pi,100);
• y = sin(x);
• figure;
• plot(x,y);
• y2 = cos(x);
• plot(x,y,x,y2);
BUT !!!
plot cont.
• plot(x,y,x,y2,'LineWidth',2);
• title('AM2
plot','FontWeight','bold','FontSize',20);
• axis tight;
• xlabel('x','FontWeight','bold','FontSize',20);
• ylabel('y','FontWeight','bold','FontSize',20);
• set(gca,'XTick',[0 pi 2*pi],'XTickLabel',
{'0','pi','2pi'});
• set(gca,'FontWeight','bold','fontsize',20);
compare two scalar
• ==
• >
• <
• >=
• <=
• true: return 1
• otherwise: return 0
• Try it
• 1==2
• 3>=2
• 1>=2
• 3<3
if…else
• grade = 65;
• if grade >= 60
• disp('Pass~');
• else
• disp('Fail!! QQ~');
• end
for loop
• grades = [60,50,40,70];
• names = {'John','Peter','Chen','Lucky'};
• for i=1:numel(grades)
• if grades(i) >= 60
• disp([names(i) ': Pass']);
• else
• disp([names(i) ': Fail']);
• end
• end
try it
• m = magic(100);
• Get row index and column index (i,j) for all the
elements which grater than 3.
some tips for speed up
•
• zeros(n,m)
• for loop
• tic;toc; %timer
• JIT compiler
•
• logical array find
N = 1e7;
tic
w = zeros(1, N);
for i = 1:N
w(i) = i*5;
end
fprintf('For loop: %.4fsn', toc);%=> 0.053s
tic
y = (1:N)*5;
fprintf('Vectorized: %.4fsn', toc);%=> 0.082s
Try it:
feature(‘accel’,’off’);
and run again
parfor
N = 1e7;
tic
w = zeros(1, N);
parfor i = 1:N
w(i) = i*5;
end
fprintf('Parfor loop: %.4fsn', toc);
%=> 1.853s
homework
• matlab
• id =
• id_last = id
• y=5sin(2/id_last*x) 2
• x 0 10pi 500
•
• AM2 MATLAB <id>
• x 0 10pi pi
• 20
• ceiba
example

Contenu connexe

Tendances

Tendances (20)

The fundamentals of regression
The fundamentals of regressionThe fundamentals of regression
The fundamentals of regression
 
C programming codes for the class assignment
C programming codes for the class assignmentC programming codes for the class assignment
C programming codes for the class assignment
 
Object-Oriented Programming in Functional Programming in Swift
Object-Oriented Programming in Functional Programming in SwiftObject-Oriented Programming in Functional Programming in Swift
Object-Oriented Programming in Functional Programming in Swift
 
Alg 2 sections 2.1
Alg 2 sections 2.1Alg 2 sections 2.1
Alg 2 sections 2.1
 
1D Array
1D Array1D Array
1D Array
 
3. chapter ii
3. chapter ii3. chapter ii
3. chapter ii
 
Lecture 2 coding_principles
Lecture 2 coding_principlesLecture 2 coding_principles
Lecture 2 coding_principles
 
Cpl
CplCpl
Cpl
 
Magic Methods (Python meetup)
Magic Methods (Python meetup)Magic Methods (Python meetup)
Magic Methods (Python meetup)
 
Calc 3.6a
Calc 3.6aCalc 3.6a
Calc 3.6a
 
4. chapter iii
4. chapter iii4. chapter iii
4. chapter iii
 
Arrays in Data
Arrays in DataArrays in Data
Arrays in Data
 
Los dskn
Los dsknLos dskn
Los dskn
 
C arrays
C arraysC arrays
C arrays
 
Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020
 
Dr.nouh part summery
Dr.nouh part summeryDr.nouh part summery
Dr.nouh part summery
 
Lecture 6 python oop (ewurc)
Lecture 6 python oop (ewurc)Lecture 6 python oop (ewurc)
Lecture 6 python oop (ewurc)
 
Comparisons
ComparisonsComparisons
Comparisons
 
Function vs not function
Function vs not functionFunction vs not function
Function vs not function
 
Vcs5
Vcs5Vcs5
Vcs5
 

En vedette

Boho Rayon Skirts
Boho Rayon SkirtsBoho Rayon Skirts
Boho Rayon SkirtsEra Chandok
 
Los símbolos patrios
Los símbolos patriosLos símbolos patrios
Los símbolos patrioscuevaquispe
 
Creating a Cloud First Standard for Your Enterprise
Creating a Cloud First Standard for Your EnterpriseCreating a Cloud First Standard for Your Enterprise
Creating a Cloud First Standard for Your EnterpriseAmazon Web Services
 
Mastering your Cloud Consumption - Pop-up Loft Tel Aviv
Mastering your Cloud Consumption - Pop-up Loft Tel AvivMastering your Cloud Consumption - Pop-up Loft Tel Aviv
Mastering your Cloud Consumption - Pop-up Loft Tel AvivAmazon Web Services
 
HISTORIA DEL COMPUTADOR DE HAROLD SUAREZ CASTRO DE 9-3
HISTORIA DEL COMPUTADOR DE HAROLD SUAREZ CASTRO DE 9-3HISTORIA DEL COMPUTADOR DE HAROLD SUAREZ CASTRO DE 9-3
HISTORIA DEL COMPUTADOR DE HAROLD SUAREZ CASTRO DE 9-3HAROLD ANDREY SUAREZ CASTRO
 
VIV Asia 2013: Enzymes in Animal Nutrition, CropTech-FeedTech Conference, co-...
VIV Asia 2013: Enzymes in Animal Nutrition, CropTech-FeedTech Conference, co-...VIV Asia 2013: Enzymes in Animal Nutrition, CropTech-FeedTech Conference, co-...
VIV Asia 2013: Enzymes in Animal Nutrition, CropTech-FeedTech Conference, co-...VIV Corporate
 

En vedette (10)

Boho Rayon Skirts
Boho Rayon SkirtsBoho Rayon Skirts
Boho Rayon Skirts
 
Redacción de textos....
Redacción de textos....Redacción de textos....
Redacción de textos....
 
Proyecto de vida
Proyecto de vida Proyecto de vida
Proyecto de vida
 
curriculum DSR-mod
curriculum DSR-modcurriculum DSR-mod
curriculum DSR-mod
 
Los símbolos patrios
Los símbolos patriosLos símbolos patrios
Los símbolos patrios
 
Capturando la acción
Capturando la acciónCapturando la acción
Capturando la acción
 
Creating a Cloud First Standard for Your Enterprise
Creating a Cloud First Standard for Your EnterpriseCreating a Cloud First Standard for Your Enterprise
Creating a Cloud First Standard for Your Enterprise
 
Mastering your Cloud Consumption - Pop-up Loft Tel Aviv
Mastering your Cloud Consumption - Pop-up Loft Tel AvivMastering your Cloud Consumption - Pop-up Loft Tel Aviv
Mastering your Cloud Consumption - Pop-up Loft Tel Aviv
 
HISTORIA DEL COMPUTADOR DE HAROLD SUAREZ CASTRO DE 9-3
HISTORIA DEL COMPUTADOR DE HAROLD SUAREZ CASTRO DE 9-3HISTORIA DEL COMPUTADOR DE HAROLD SUAREZ CASTRO DE 9-3
HISTORIA DEL COMPUTADOR DE HAROLD SUAREZ CASTRO DE 9-3
 
VIV Asia 2013: Enzymes in Animal Nutrition, CropTech-FeedTech Conference, co-...
VIV Asia 2013: Enzymes in Animal Nutrition, CropTech-FeedTech Conference, co-...VIV Asia 2013: Enzymes in Animal Nutrition, CropTech-FeedTech Conference, co-...
VIV Asia 2013: Enzymes in Animal Nutrition, CropTech-FeedTech Conference, co-...
 

Similaire à AM2 MATLAB

Similaire à AM2 MATLAB (20)

Matlab introduction
Matlab introductionMatlab introduction
Matlab introduction
 
Lines and planes in space
Lines and planes in spaceLines and planes in space
Lines and planes in space
 
Mat lab workshop
Mat lab workshopMat lab workshop
Mat lab workshop
 
Matlab-1.pptx
Matlab-1.pptxMatlab-1.pptx
Matlab-1.pptx
 
Mat lab day 1
Mat lab day 1Mat lab day 1
Mat lab day 1
 
matlab_tutorial.ppt
matlab_tutorial.pptmatlab_tutorial.ppt
matlab_tutorial.ppt
 
matlab_tutorial.ppt
matlab_tutorial.pptmatlab_tutorial.ppt
matlab_tutorial.ppt
 
matlab_tutorial.ppt
matlab_tutorial.pptmatlab_tutorial.ppt
matlab_tutorial.ppt
 
Matlab introduction
Matlab introductionMatlab introduction
Matlab introduction
 
MATLAB - Arrays and Matrices
MATLAB - Arrays and MatricesMATLAB - Arrays and Matrices
MATLAB - Arrays and Matrices
 
Image processing
Image processingImage processing
Image processing
 
Introduction to Matlab - Basic Functions
Introduction to Matlab - Basic FunctionsIntroduction to Matlab - Basic Functions
Introduction to Matlab - Basic Functions
 
Introduction to MATLAB
Introduction to MATLABIntroduction to MATLAB
Introduction to MATLAB
 
Intro matlab and convolution islam
Intro matlab and convolution islamIntro matlab and convolution islam
Intro matlab and convolution islam
 
INTRODUCTION TO MATLAB presentation.pptx
INTRODUCTION TO MATLAB presentation.pptxINTRODUCTION TO MATLAB presentation.pptx
INTRODUCTION TO MATLAB presentation.pptx
 
Mbd2
Mbd2Mbd2
Mbd2
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 
Introduction to matlab lecture 2 of 4
Introduction to matlab lecture 2 of 4Introduction to matlab lecture 2 of 4
Introduction to matlab lecture 2 of 4
 
An Introduction to MATLAB for beginners
An Introduction to MATLAB for beginnersAn Introduction to MATLAB for beginners
An Introduction to MATLAB for beginners
 
bobok
bobokbobok
bobok
 

Dernier

Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 

Dernier (20)

Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 

AM2 MATLAB

  • 2. Matalb • => • => • => i.e. integer -> double • •
  • 6. command • lookfor <keyword> • help <command> • a=1 • a=2; • whos a • clear 0 • %
  • 7. simple calculator • a = 1 • b = a+1 • c = a + b • d = a*b^c - c/b
  • 8. predefined variables • pi %=> 3.14…… • i or j %=> 0.0000+1.0000i • %Tips: use 1i
  • 9. variable declaration • s => undefined • scalar: s=1 • vector: • v1=[1,2,3] • v1=1:3 • v2=[1;2;3] • v2=[1,2,3]' • matrix • m1=[1,2;3,4] 1 2 3 4 row 1 row 2 col 1 col 2
  • 10. vector indexing • v=1:2:10 • v(5) • v(1:3) • v(:) • v(end) • v(1:end~=3) • Try it: • Get the 2nd element from v. • Get 3rd to end elements. • Put 1st, 3rd, 5th element as a new array named b.
  • 11. matrix indexing • matrix • m=[1:3;4:6;7:9] 1 2 3 4 5 6 7 8 9 • matrix • m=[1:3;4:6;7:9] • m(1,:) % get 1st row • m(:) % get all element • m(1:2,3) % submatrix • m(8) ??? • linear indexing • m([1,4,5,6])
  • 12. try it • m = magic(5) • Get first column from m. • Get 3~5 rows from m. • Get submatrix which formed by the intersection of 1~3 rows and 2~4 columns of m and store in a new matrix m1(3x3 matrix). • Get a column vector which formed by all the odd linear index elements in m.
  • 13. operator • matrix operatior • * • / • ^ • ….. • element-wise operator • .* • ./ • .^ • …..
  • 14. try it • v1=1:3 • v2=v1' • v3=v1*2 • v3*v2 • v2*v3 • v1.*v3 • v1.^v3 • m1=[1:3;4:6] • m2=m1' • m3=m1*2 • m3*m2 • m2*m3 • m1.*m3 • 10.^m1
  • 15. matlab function • v=1:20; • sin(v) • cos(v), tan(v)…. • exp(v), log(v)… • diag, eig( ) • sum( ) • check how to use: help <function>
  • 16. try it • m = magic(100); • Get following arrays: • 1st row • 25th column • diagonal • Get the summation of above arrays. Are they the same?
  • 17. example 1: eigenvalue problem • A=magic(5) • [vectors, values] = eig(A) • isequal(A*vectors,vectors*values) %=> ? • A*vectors-vectors*values %=> ?
  • 18. example 2: make a plot • linspace(<start>,<end>,<sep>) • x = linspace(0,2*pi,100); • y = sin(x); • figure; • plot(x,y); • y2 = cos(x); • plot(x,y,x,y2);
  • 19.
  • 21.
  • 22. plot cont. • plot(x,y,x,y2,'LineWidth',2); • title('AM2 plot','FontWeight','bold','FontSize',20); • axis tight; • xlabel('x','FontWeight','bold','FontSize',20); • ylabel('y','FontWeight','bold','FontSize',20); • set(gca,'XTick',[0 pi 2*pi],'XTickLabel', {'0','pi','2pi'}); • set(gca,'FontWeight','bold','fontsize',20);
  • 23.
  • 24. compare two scalar • == • > • < • >= • <= • true: return 1 • otherwise: return 0 • Try it • 1==2 • 3>=2 • 1>=2 • 3<3
  • 25. if…else • grade = 65; • if grade >= 60 • disp('Pass~'); • else • disp('Fail!! QQ~'); • end
  • 26. for loop • grades = [60,50,40,70]; • names = {'John','Peter','Chen','Lucky'}; • for i=1:numel(grades) • if grades(i) >= 60 • disp([names(i) ': Pass']); • else • disp([names(i) ': Fail']); • end • end
  • 27. try it • m = magic(100); • Get row index and column index (i,j) for all the elements which grater than 3.
  • 28. some tips for speed up • • zeros(n,m) • for loop • tic;toc; %timer • JIT compiler • • logical array find
  • 29. N = 1e7; tic w = zeros(1, N); for i = 1:N w(i) = i*5; end fprintf('For loop: %.4fsn', toc);%=> 0.053s tic y = (1:N)*5; fprintf('Vectorized: %.4fsn', toc);%=> 0.082s Try it: feature(‘accel’,’off’); and run again
  • 30. parfor N = 1e7; tic w = zeros(1, N); parfor i = 1:N w(i) = i*5; end fprintf('Parfor loop: %.4fsn', toc); %=> 1.853s
  • 31. homework • matlab • id = • id_last = id • y=5sin(2/id_last*x) 2 • x 0 10pi 500 • • AM2 MATLAB <id> • x 0 10pi pi • 20 • ceiba