130 likes | 151 Views
CS100: Into to Computer Science. Introduction to MatLab. MatLab stands for Matrix Laboratory. As the name suggests most of the programming operations have as input or output a matrix or a vector. Prepared by Fred Annexstein University of Cincinnati Some Rights Reserved. Overview.
E N D
CS100: Into to Computer Science Introduction to MatLab MatLab stands for Matrix Laboratory. As the name suggests most of the programming operations have as input or output a matrix or a vector. Prepared by Fred Annexstein University of Cincinnati Some Rights Reserved
Overview Like Scratch, MatLab is an interpreted programming language. Unlike Scratch, Matlab is a language in which you type commands at the prompt. Try these MATRIX AND VECTOR OPERATIONS This is how we can define a vector >> v=[1, 2, 3] Matlab prints out the following v = 1 2 3 Similarly we can define a matrix >> M= [ 1 2 3; 4 5 6; 7 8 9] The result is: M = 1 2 3 4 5 6 7 8 9 If you want to suppress the MatLab output then you need to finish the line with semicolon as follows. >>M= [ 1 2 3; 4 5 6; 7 8 9];
To find the sum of the columns of M we can use the 'sum' function. >> sum(M) ans = 12 15 18 To find the sum of all elements use >> sum(sum(M)) ans = 45 Note that the 'sum' function takes both matrices and vectors as parameters. The inner 'sum' takes a matrix and outputs a vector which is the input for the outer 'sum'. By default the output is assigned to the 'ans' variable unless we assign the output explicitly as follows. >> s=sum(M) s = 12 15 18
>> result= s+v result = 13 17 21 Note that some operations can produce long output. Typing 'more on' will switch to paging mode which controls the output dump. To perform a component-by-component operation on a vector or matrix you need to prepend the arithmetic operator by a '.' >> M .* M ans = 1 4 9 16 25 36 49 64 81 Note that this squares each element of the matrix. The result is different from Usual squaring the matrix as shown below. >> M*M ans = 30 36 42 66 81 96 102 126 150
Projection Say you want to extract some rows and columns of a matrix. This is called a projection. We simply give the subset of rows and columns as parameters, as follows >> M11=M(2:3 , 2:3) M11 = 5 6 8 9 To specify all elements in a given dimension one can use ':‘ So to get all rows but just columns 1 and 2, we type >> A= M( :, 1:2) A = 1 2 4 5 7 8
Building FUNCTIONS Each function you create should be separated in a *.m file and should have the same name as the file. Create an m-file by going to the menu option File>New>M-file The syntax for defining a function is: function result= myfunction(A,b) where A and b are parameters and result is the variable whose value is returned as a result from the function. myfunction is an arbitrary function name. Let's write and save a function to calculate a simple polynomial. (put the following two line in a file named mypoly.m) function y=mypoly(x) y= 3*(x.^2)+ 4*x + 5; At the MatLab prompt type the following which gives us 800 points on the real line interval from -4 to +4, with each pair of points separated by 0.01 >> x= -4:0.01:4;
Plots Let us evaluate our polynomial along the interval we just defined >> y=mypoly(x); Now to plot the function we simply type at the prompt >> plot(x,y) This will pop up a graphical window with the plot of y=3x^2 +4x +5 in the interval [-4; 4]. If for some reason we need to overlay two plots on the same graph the command 'hold on' can be used to hold the current display and show all subsequent plots on top of the first one. 'hold off' turns that capability off. >>hold on >>plot(x, 50*sin(x))
3D PLOTS and SURFACES Let us first draw a 2-D circle: >>cosx=cos(x); >>sinx=sin(x); >>plot(cosx,sinx) We can use the same idea in 3D to build a helix: >> plot3(cosx,sinx,x); Exercise: Usually the third dimension is called z, so create a z-axis from 0 to 100 and plot a helix along this axis.
3D PLOTS and SURFACES SURFACES >> x=1:10; >> y=1:10; Create a 10x10 matrix using outer product operation (here we are using the transpose of a vector x) >> Z=x'*y; Note that here Z is a matrix holding the z value for every combination of x and y. Look at the matrix values as a surface. >> surf(x,y,Z); How about the surface of the sine function >>S=sin(Z); >> surf(x,y,S); % Add some labels and other fancy stuff xlabel('X'); ylabel('Y'); zlabel('Z'); title('My Plot');
Loops in MatLab MatLab has all the control we have seen in Excel and Scratch, including if then-else statements, repeat loops and for loops. If possible avoid using 'for' loops. Try to rewrite the computation in matrix form. Because MatLab is interpreted 'for' loops take a lot of time. Try these examples: x=0; for i=1:31 y(i)= sin(x); x=x+0.1; end Can also be rewritten as: x2=0:0.1:3; y2=sin(x2);
CELL ARRAYS in MatLab Matrices and vectors can have only elements of the same type. They also have fixed dimensions. To overcome this limitation cell arrays are introduced. C= cell(3,2) C{1,1}=[1,2]; C{1,2}=[3,4]; C{2,1}=[5 6; 7 8]; C{2,2}=[ 9 10 11; 12 13 14]; C{3,1}= ones(3); C{3,2}='Hello'; To access an element type: >> C{3,1} ans = 1 1 1 1 1 1 1 1 1 >> C{2,2}(1,3) ans = 11 Note the use of the two different kinds of parentheses. The first one '{}' accesses the cell array elements. The second one '()' accesses the elements of the object contained in the cell array.
WORKING WITH IMAGES in MatLab Let’s talk about image files and their formats….. Color vs GrayScale Basic functions: img=imread('image1.jpg'); imwrite(img, 'image2.jpg'); IMGJPG=imread(‘face.jpg’); IMGTIFF=imread('face1.tiff'); imshow(IMGJPG); Example Program: Inverting an image %To invert or to add two images we need to convert to double and % then rescale the result back so that it looks like an image %This does not work: RES= 255 - IMG1; RES= 1 - double(IMG1)/255; figure imshow ( uint8(round(RES*255)) ) Exercise: Find 2 images online and add them together.
WORKING WITH IMAGES in MatLab RESIZE >> img=imread('ric.jpg'); >> size(img) ans = 435 312 3 >> rowV=linspace(1,435,312); >> newrows=round(rowV); >> newIMG=img(newrows,:,:) >> image(newIMG); Exercise: Finally when you are done, save your workspace and turn in on Bb. Next time: More on Images, including filtering and Edge detection