introduction to matlab for biomedical engineering bme 1008 introduction to biomedical engineering...

27
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)

Upload: jade-blakley

Post on 14-Dec-2015

226 views

Category:

Documents


1 download

TRANSCRIPT

Introduction to MATLAB for Biomedical Engineering

BME 1008 Introduction to Biomedical EngineeringFIU, Spring 2015

Lesson 2:Element-wise vs. matrix operationssave(), load(), matlab scriptplot(x,y)

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 : 5x2 = -2 -1 0 1 2 3 4 5

General colon (:) syntax: initial value : increment : final value

>> x3 = 1:1.5:6x3 =

1.0 2.5 4.0 5.5

>> x4 = 1:6 %default increment 1x4 =

1.0 2.0 3.0 4.0 5.0 6.0

In matrix algebra

In MATLAB A*B means matrix productA .* B means element-by-element multiplication' . ' (dot) - indicates element-by-element operation

Matrix product

C = AB

Element-wise (array) operators:+, -, .* , ./, .^

'+ ' - addition always element wise>> x1 = [1 2 3]; x2 = [4 5 6];>> x1 + x2 ans =

5 7 9

>> x1 + 10ans =

11 12 13

>> x1 + [10 20]??? Error using ==> plusMatrix 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

'.* ' - element-wise multiplication>> x1 = [1 2 3]; x2 = [4 5 6];>> x1 .* x2 ans =

4 10 18

>> x1 * 10ans =

11 12 13

>> x1*x2??? Error using ==> mtimesInner matrix dimensions must agree.

'./ ' - element-wise multiplication>> x1 = [1 2 3]; x2 = [4 5 6];>> x1 ./ x2 ans =

0.25 0.4 0.5

>> 1./x1ans =

1.0 0.5 0.33

>> 1/x1??? Error using ==> mrdivideMatrix dimensions must agree.

'.^ ' - element-wise power>> A = [5 1 -2];

>> A .^ 3 ans =

125 1 -8

>> A ^ 3??? Error using ==> mpowerMatrix must be square.

Matrix operators *, ^>> x1 = [1 2 3]; %row vectorx1 =

1 2 3>> x2 = [4 5 6]'; %column vector x2 =

456

>> x1*x2 %matrix multiplicationans =

32

>> x1 = [1 2; 3 4] % 2-by-2 matrixx1 =

1 23 4

>> x1^2 % or x1*x1 ans =

7 1015 22 %matrix product

Quiz 11) What is the average of:

13, 1.53, 23, 2.53..., 11.53, 123 ?Hint: First create vector, then cube.

2) What is the maximum element of C, where

Hint: Use max() two times.

128

63,

72

5.530, BAABC

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

489.1250

2) >> A=[30 5.5; 2 -7]; B=[-3 6; 8 12]; >> C = A*BC =

-46 246 -62 -72>> max(C) %returns max. element from each columnans =

-46 246>> max(max(C)) %returns max. element from Cans =

246

Matlab Scalar functionssin(), cos(), exp(), log(), log10() operate element-wise

>> t = [0 : pi/2: 2*pi ] % pi = 3.14 Matlab constantt =

0 1.57 3.14 4.71 6.28>> y = sin(t) y =

0 1 0 -1 0

>> e = exp([0 1 -1]) %exponential functione =

1.00 2.71 0.36

SAVE & LOAD Commandssave('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'

>> 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

Manual Loading & Saving

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

Matlab scripttext file with a series of Matlab commands Matlab program with no input and output argumentshas extension '.m' (M-file)

(1) New or Open

(2) Edit inMatlab editor

(4) Run(3) Save

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

2000 2002 2004 2006 2008 2010 20120

2000

4000

6000

8000

10000

12000

14000

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')

2000 2005 2010 20150

5000

10000

15000

year

tran

sist

or c

ount

(m

illio

n)

Moore's law

Figure editing(1) Edit plot

(3) Change properties

(2) Select object(line, axis, label)

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

Copy & Paste figure to Word or PPT

Quiz 2

Given y = sin(x) × e-x/15 and x = [0:0.2:10*pi]hint: exp()max()

plot y vs. xfind maximum y

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

0 10 20 30 40-1

-0.5

0

0.5

1