This preview shows page 1. Sign up to view the full content.
Unformatted text preview: Matlab Workshop
Msc Advanced Neuroimaging 2009/2010
Marc (email@example.com) and Jorge (firstname.lastname@example.org) What is Matlab? Matrix Laboratory by “The Mathworks, Inc” (very) High level programing language Includes several libraries to simplify our life Includes toolbox for speciﬁc use (Image Processing for example) Cross-platform What does it look like? Small things to know before doing anything How to navigate within folder >> pwd >> dir (or ls) >> cd .. >> cd name How to get some help >> help <functionName> gives a brief description of a speciﬁc function >> doc <functionName> returns a complete description Consult the help and doc of random function (example: max, min, median, ...) Variables the name begins with a letter the name can be a mix of letter, digit, ... the name is case sensitive the name has 31 characters maximum they can be string, scalar, vector, matrix, ... Their index starts at one Create a variable of each type, as in the example bellow. Why did we use “;” after each command? Use some common operators (“+”, “-”, “*”, “/”) on the variables you just created. m-ﬁle As there are some matlab functions (min, max, mean, ...), we could create your own. They are declared into m-ﬁles. A m-ﬁle could has well contains a script. Bellow is the example of a function which takes 1 scalar as an input and returns a third of the value. function result = third(inputValue) % This function returns one third of the input. result = inputValue / 3; The created function can then be called from the Command Window, such as: >> var = 345; >> test = third(var); >> disp(test) 115 Create a function which take 5 values as an input and return the mean of the minimum and the maximum values. Create a second function which generate a random value between 0 and 100 and return the square of this value. (>> help rand). Complex Number Use i or j to deﬁne the imaginary part >> a=2+3*i or a=2+3i or a=2+i*3 2.0000 + 3.0000i Why >> a+i3 does not work ? >> help complex to avoid confusion We can use “+”, “-”, “*” or “/” Create a function which takes an argument and a module as input and return the corresponding complex number. Reminder z = a + ib |z | = b arg(z ) = tan−1 ( ) a a2 + b2 Array and plotting The following function ﬁll an array (vector) such as: array(i)=log(i/100) with i ∈ ]0;1000], and plot the values as a curve. Logarithm function
3 2 function functionName(start,stop) for i=start:stop array(i)=log(i/100); absc(i)=i/100; end plot(absc,array); 1 0 −1 −2 −3 −4 −5 0 1 2 3 4 5 6 7 8 9 10 The same output could have been obtained using the following commands: >> absc = 1/100:1/100:10; >> array = log(absc); >> plot(absc, array); Start by reproducing the logarithm function plot. What happen if we donʼt specify the abscise values? What is the command to display a plot title? The following ﬁgure presents four different functions at once. Using the plot function documentation, reproduce it. (Each function is represented by a color)
Four different functions (log, round, sqrt, cos) gathered in one graph 10 logarithm function round function sqrt function cosine function 5 0 −5 0 1 2 3 4 5 6 7 8 9 10 The following plot has been created using the associated script. Reproduce and understand it. 35 30 25 20 15 10 5 0 1 0.5 0 −0.5 −1 −1 −0.5 0.5 0 1 t = 0:pi/50:10*pi; plot3(sin(t),cos(t),t) grid on axis square The following circle is a simpliﬁcation of the previous helix using the function plot. Reproduce it.
Regular circle 1 0.8 0.6 0.4 0.2 0 −0.2 −0.4 −0.6 −0.8 −1 −1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1 The following plot has been created using 1000 random x-axis values between 0 and 100 and 1000 random y-axis values in the same range (>>help rand). Reproduce it. Then compute the barycenter of all these points and display it with a different color.
100 90 80 70 60 50 40 30 20 10 0 0 10 20 30 40 50 60 70 80 90 100 Save / Load A variable can be save on the hard drive in order to be used later. Consult the documentation about it. Create then an array which contains 1,000,000 random value between 0 and 100. Save the array in your own folder. Then compute the max, min, mean, median, 10 and 90 percentiles using only the ﬁrst 10, 100, 1000, 10000, 100000 values and then all values. How do the values evolve? Use the plot function to visualize the trend. ...
View Full Document
- Spring '10