New version page

UCSD CSE 152 - Assignment

This preview shows page 1-2-3 out of 9 pages.

View Full Document
View Full Document

End of preview. Want to read all 9 pages?

Upload your study docs or become a GradeBuddy member to access this document.

View Full Document
Unformatted text preview:

CSE 152 Assignment 0Spring 2007Due Tuesday, April 10, 5:00 PMInstructions:• Turn in a hardcopy of the completed tutorial, your Part 2 writeup, your Matlab code, andboth pairs of images (source & result) in class.• Email your Matlab source code and results (attach as a zip file) to [email protected] the title CSE 152 Assignment 0.Description:The purpose of this assignment is to gain some familiarity with Matlab programming. Matlab isintuitive and easy to use! Even if you don’t understand a command or a feature of the language,you can simply consult the reference manual that comes with the program.Part 1 Print out the Matlab tutorial portion of the assignment and check each section off once youhave completed it. You should not turn in any Matlab output. You may also make commentsnext to any portion that caused you trouble, or to make suggestions that would be useful forfuture revisions of the tutorial.Part 2 Make your own artistic album cover! Using your newfound Matlab skills, write a programthat does the following:• Read in an image• Resize the image to 256 × 256 pixels using bilinear interpolation• Tile the image to form 4 quadrants, where◦ The top left quadrant is the original image◦ The top right is the red channel of the original image◦ The bottom left is the green channel◦ The bottom right is the blue channelTest your program with the given image flag.jpg. Your program should be short (5 to 10lines), and your result should match Figure 1. Then, write a short paragraph explaining yourresults. Does your program produce the correct output? Does the red / green / blue channelseparation make sense?Finally, try running your program with a picture of yourself! (Note that if your image di-mensions are not square, the resulting picture may be distorted, so you may want to cropthe image appropriately.) Turn in a copy of the source image and the final result with theassignment.1Figure 1: The source image and result for flag.jpg2Matlab TutorialAdapted from http://www-cse.ucsd.edu/~sjb/classes/matlab/matlab.intro.html.See http://www-cse.ucsd.edu/~sjb/classes/matlab/ for more Matlab related resources.1 Help and basics• The symbol “%” is used in front of a comment.• To get help type “help” (will give list of help topics) or “help topic”• If you don’t know the exact name of the topic or command you are looking for, type“lookfor keyword” (e.g., “lookfor regression”)• When writing a long matlab statement that exceeds a single row use “...” to continuestatement to next row.• When using the command line, a “;” at the end means matlab will not display the result. If“;” is omitted then matlab will display result.• Use the up-arrow to recall commands without retyping them (and down arrow to go for-ward in commands).• Other commands borrowed from emacs and/or tcsh: C-a moves to beginning of line (C-e for end), C-f moves forward a character (C-b moves back), C-d deletes a character, C-kcuts the line to the right of the cursor, C-u deletes an entire line, C-p goes back through thecommand history and C-n goes forward (equivalent to up and down arrows), tab commandcompletion.2 Objects in MatlabThe basic objects in matlab are scalars, vectors, and matrices.N = 5 % a scalarv = [1 0 0] % a row vectorv = [1;2;3 ] % a column vectorv = v’ % tra n s pose a vector% ( row to column or column to row)v = [1: .5:3] % a vector in a speci f ied range :v = pi *[ -4:4]/4 % [ start : st e p size : end ]v = [] % empty vectorm = [1 2 3; 4 5 6] % a matrix : 1 ST param e t er is ROWS3% 2 ND par a m eter is COLSm = zeros (2 ,3) % a matrix of z eros : often MATLAB will run% faster if you prea l loca t e the matrix% ( e. g. filling it with zeros ) before% using it .v = ones (1 ,3) % a matrix of onesm = eye (3) % iden t i ty matrixv = rand (3 ,1) % rand matrix ( see also r a ndn )load matrix_d a ta % read data from a fi le :% create a file ’ matrix_data ’ con t aini n g :% 2 3 4% 5 6 7% 1 2 3ma t rix_datav = [1 2 3]; % access a vector elementv (3) % vector ( number )m = [1 2 3; 4 5 6]m (1 ,3) % access a matrix element% matrix ( rownumbe r , c o lum n numb e r )m (2 ,:) % access a matrix row (2 nd row )m (: ,1) % access a matrix column (1 st row )size ( m ) % size of a matrixsize (m ,1) % number rowssize (m ,2) % number of columnsm1 = zeros ( size (m )) % create a new matri x w ith size of mwho % list of var i a bleswhos % list / size / type of v ariab l es43 Simple operations on vectors and matrices3.1 Pointwise (element by element) OperationsAddition of vectors/matrices and multiplication by a scalar are done “element by element.”a = [1 2 3 4]; % vector2 * a % scalar m ult i pli cat i ona / 4 % scalar m ult i pli cat i onb = [5 6 7 8]; % vectora + b % poi n t wise vector ad d i tiona - b % poi n t wise vector ad d i tiona .^ 2 % pointise vector s q uaring ( note .)a .* b % pointw i se vect o r multi p l y ( note .)a ./ b % pointw i se vect o r divide ( note .)log ( [1 2 3 4] ) % pointwi s e a r ithm e t ic operat i onround ( [1.5 2; 2.2 3.1] ) % poi n t wise ari t h meti c op e ratio n3.2 Vector Operations (no for loops needed)Built-in matlab functions operate on vectors, but if a matrix is given then the function operates oneach column of the matrix.a = [1 4 6 3] % vectorsum ( a ) % sum of vector el e m entsmean ( a ) % mean of vector e l ementsvar ( a ) % varianc estd ( a ) % standar d dev i ationmax ( a ) % maximuma = [1 2 3; 4 5 6] % matrixmean ( a ) % mean of each colum nmax ( a ) % max of each columnmax ( max ( a ) ) % to obtain max of matrixmax ( a (:) ) % or ...3.3 Matrix Operations[1 2 3] * [4 5 6]’ % row vector 1 x3 times c o l u m n vector 3 x1% results in single number , also% known as dot p r o d uct or inner product[1 2 3]’ * [4 5 6] % column vector 3 x1 times row vector 1 x3% results in 3x3 matrix , also% known as outer p r o d uct5a = rand (3 ,2) % 3 x2 m a trixb = rand (2 ,4) % 2 x4 m a trixc …


View Full Document
Loading Unlocking...
Login

Join to view Assignment and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Assignment and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?