Download presentation

Presentation is loading. Please wait.

Published byJade Blakley Modified about 1 year 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 = [ ] x = Using colon (:) notation >> x2 = -2 : 5 x2 =

3
General colon (:) syntax: initial value : increment : final value >> x3 = 1:1.5:6 x3 = >> x4 = 1:6 %default increment 1 x4 =

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 = >> x ans =

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 - x1 ans = 2 1 0

7
'.* ' - element-wise multiplication >> x1 = [1 2 3]; x2 = [4 5 6]; >> x1.* x2 ans = >> x1 * 10 ans = >> 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 = >> 1./x1 ans = >> 1/x1 ??? Error using ==> mrdivide Matrix dimensions must agree.

9
'.^ ' - element-wise power >> A = [5 1 -2]; >> A.^ 3 ans = >> 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 = >> x1*x2 %matrix multiplication ans = 32

11
>> x1 = [1 2; 3 4] % 2-by-2 matrix x1 = >> x1^2 % or x1*x1 ans = %matrix product

12
Quiz 1 1) What is the average of: 1 3, 1.5 3, 2 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 =

14
2) >> A=[30 5.5; 2 -7]; B=[-3 6; 8 12]; >> C = A*B C = >> max(C) %returns max. element from each column ans = >> 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 = >> y = sin(t) y = >> e = exp([0 1 -1]) %exponential function e =

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 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= [ ]; 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 =

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google