Download presentation

Presentation is loading. Please wait.

Published byJade Blakley Modified over 3 years ago

1
Introduction to MATLAB for Biomedical Engineering BME 1008 Introduction to Biomedical Engineering FIU, Spring 2015 Lesson 2: Element-wise vs. matrix operations save(), load(), matlab script plot(x,y)

2
Creating Vectors (1D arrays) Explicit list of elements >> x1 = [-2 -1 0 1 2 3 4 5] x = -2 -1 0 1 2 3 4 5 Using colon (:) notation >> x2 = -2 : 5 x2 = -2 -1 0 1 2 3 4 5

3
General colon (:) syntax: initial value : increment : final value >> x3 = 1:1.5:6 x3 = 1.0 2.5 4.0 5.5 >> x4 = 1:6 %default increment 1 x4 = 1.0 2.0 3.0 4.0 5.0 6.0

4
In matrix algebra In MATLAB A*B means matrix product A.* B means element-by-element multiplication '. ' (dot) - indicates element-by-element operation Matrix product C = AB

5
Element-wise (array) operators: +, -,.*,./,.^ '+ ' - addition always element wise >> x1 = [1 2 3]; x2 = [4 5 6]; >> x1 + x2 ans = 5 7 9 >> x1 + 10 ans = 11 12 13

6
>> x1 + [10 20] ??? Error using ==> plus Matrix dimensions must agree. '- ' - subtraction always element wise >> x1 = [1 2 3]; x2 = [4 5 6]; >> x1 - x2 ans = -3 -3 -3 >> 3 - x1 ans = 2 1 0

7
'.* ' - element-wise multiplication >> x1 = [1 2 3]; x2 = [4 5 6]; >> x1.* x2 ans = 4 10 18 >> x1 * 10 ans = 11 12 13 >> x1*x2 ??? Error using ==> mtimes Inner matrix dimensions must agree.

8
'./ ' - element-wise multiplication >> x1 = [1 2 3]; x2 = [4 5 6]; >> x1./ x2 ans = 0.25 0.4 0.5 >> 1./x1 ans = 1.0 0.5 0.33 >> 1/x1 ??? Error using ==> mrdivide Matrix dimensions must agree.

9
'.^ ' - element-wise power >> A = [5 1 -2]; >> A.^ 3 ans = 125 1 -8 >> A ^ 3 ??? Error using ==> mpower Matrix must be square.

10
Matrix operators *, ^ >> x1 = [1 2 3]; %row vector x1 = 123 >> x2 = [4 5 6]'; %column vector x2 = 4 5 6 >> x1*x2 %matrix multiplication ans = 32

11
>> x1 = [1 2; 3 4] % 2-by-2 matrix x1 = 12 34 >> x1^2 % or x1*x1 ans = 7 10 15 22 %matrix product

12
Quiz 1 1) What is the average of: 1 3, 1.5 3, 2 3, 2.5 3..., 11.5 3, 12 3 ? Hint: First create vector, then cube. 2) What is the maximum element of C, where Hint: Use max() two times.

13
Answers to Quiz 1 1) >> x = [1:0.5:12]; y = x.^3; >> average = mean(y) average = 489.1250

14
2) >> A=[30 5.5; 2 -7]; B=[-3 6; 8 12]; >> C = A*B C = -46 246 -62 -72 >> max(C) %returns max. element from each column ans = -46 246 >> max(max(C)) %returns max. element from C ans = 246

15
Matlab Scalar functions sin(), cos(), exp(), log(), log10() operate element-wise >> t = [0 : pi/2: 2*pi ] % pi = 3.14 Matlab constant t = 0 1.573.144.716.28 >> y = sin(t) y = 0 1 0 -1 0 >> e = exp([0 1 -1]) %exponential function e = 1.00 2.71 0.36

16
SAVE & LOAD Commands save('filename') saves all workspace variables to disk in current directory in the file 'filename.mat' '.mat' - default extension for Matlab binary file (MAT-file) save('filename','var1','var2') saves only variables 'var1' and 'var2' load('filename') loads workspace variables from disk from the file 'filename.mat'

17
>> clear %removes variables from workspace >> x = [0: 0.1 : 1000]; >> y = exp(-x/500); % x, y vectors with 10000 elements >> save('myResults') >> save('myResults_y','y') >> clear >> whos >> load('myResults') >> whos

18
Manual Loading & Saving

19
Importing data from other file formats (.txt,.xls,.jpg,...)

20
Matlab script text file with a series of Matlab commands Matlab program with no input and output arguments has extension '.m' (M-file) (1) New or Open (2) Edit in Matlab editor (4) Run(3) Save

21
plot(x,y) command % Moore's Law -transistor count double every two years transistors= [50 100 200 400 800 1600... 3200 6400 12800]; years=[2000:1.5:2012]; plot(years, transistors)

22
plot(years, transistors,'r*--') %'r*--' red, star, dashed line ylabel('transistor count (million)') % y-axis label xlabel('year') % x-axis label title('Moore''s law')

23
Figure editing (1) Edit plot (3) Change properties (2) Select object (line, axis, label)

24
Saving figures '.fig' file, Matlab format, figure can be edited as image file, e.g. '.bmp'

25
Copy & Paste figure to Word or PPT

26
Quiz 2 Given y = sin(x) × e -x/15 and x = [0:0.2:10*pi] hint: exp() max() plot y vs. x find maximum y

27
Answer to Quiz 2 >> x = [0:0.2:10*pi]; >> y = sin(x).*exp(-x/15); >> figure %opens new figure window >> plot(x,y) >> y_max = max(y) y_max = 0.8984

Similar presentations

OK

Introduction to Matlab Matlab is a software package for technical computation. Matlab allows you to solve many numerical problems including - arrays.

Introduction to Matlab Matlab is a software package for technical computation. Matlab allows you to solve many numerical problems including - arrays.

© 2018 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google

Ppt on current trends in hrm Ppt on 5g technology Ppt on economic order quantity model Ppt on acid bases and salts for class 7 Ppt on chromosomes and genes diagram Ppt on tcp ip protocol tutorial Ppt on arunachal pradesh culture Ppt on conceptual art sculpture Ppt on angle subtended by an arc of a circle Ppt on environmental protection act 1986