Docsity

Matlab Examples: Math, Vectors, Matrices, Plotting, Scripting, Images, Data Fitting, Loops, Exercises of Cellular and Tissue Engineering

Toggle sidebar
bg1
Useful Matlab Examples
Basic Math
Addition: 2 + 3
Division (using previous answer): ans/4
Exponentiation: 31 � 4
Trig functions: log(100); sin(90 � pi/180)
Vectors
Assigning values: x = [0 : 1 : 5]; x = [7.5 : 2 : 33]
Taking a subset of the vector: x(2 : 5)
Assigning a single value: x(4) = x(3) + pi
Flipping between row and column vectors: x�
Reversing a row vector: flipdim(x, 2)
Squaring individual elements: y = x. � 2
Matrices
Assignment: x = [1 2 3 ; 3 1 2 ; 2 3 1]; x(2, 2) = 6
Math: x/4
Exponentiation: x. � 2
Matrix multiplication: x � 2
Extracting vectors: x(1, :); x(:, 1)
Plotting
2D plots: plot(x, y)
Turn on/off plot overlays: hold off; hold on
3D plots: image(x); surf(x); mesh(x)
1
docsity.com
pf2

Partial preview of the text

Download Matlab Examples: Math, Vectors, Matrices, Plotting, Scripting, Images, Data Fitting, Loops and more Exercises Cellular and Tissue Engineering in PDF only on Docsity!

Useful Matlab Examples

Basic Math

Addition: 2 + 3 Division (using previous answer): ans/ 4 Exponentiation: 31 � 4 Trig functions: log(100); sin(90 � pi/180)

Vectors

Assigning values: x = [0 : 1 : 5]; x = [7. 5 : 2 : 33] Taking a subset of the vector: x(2 : 5) Assigning a single value: x(4) = x(3) + pi Flipping between row and column vectors: x� Reversing a row vector: flipdim(x, 2) Squaring individual elements: y = x. � 2

Matrices

Assignment: x = [1 2 3 ; 3 1 2 ; 2 3 1]; x(2, 2) = 6 Math: x/ 4 Exponentiation: x. � 2 Matrix multiplication: x � 2 Extracting vectors: x(1, :); x(:, 1)

Plotting

2D plots: plot(x, y) Turn on/off plot overlays: hold off; hold on 3D plots: image(x); surf(x); mesh(x) 1

docsity.com

Scripting

Create a file with the name of your script, ending in .m; e.g., meanvar.m: function [meanval,varval] = meanvar(data) % Computes the mean and variance of values in the data array meanval = mean(data); varval = var(data);

Images

Reading an image: a0 = imread(�c : image.bmp�,�^ bmp�); Displaying an image: image(a0) Assigning a subset of one color: b0 = a0(1 : 445 , 240 : 420 , 2);

Data Fitting

Erf function: y = (erf((x − mean)/stdev) + 1) � range/ 2 + offset Computing error of fit: err = sum((double(b0(200, :)) − y). � 2) Minimizing an error function with respect to one variable: q = fminsearch(@(x) sqerferr(x, b0(200, :)), x0)

Loops

Looping across all rows of an array: fori = 1 : size(b0, 1) q = fminsearch(@(x) sqerferr(x, b0(i, :)), x0); stdev0(i) = q(2); end 2

docsity.com