1 / 12

COMP 116: Introduction to Scientific Programming

COMP 116: Introduction to Scientific Programming . Lecture 3: Arrays and Matrices. Recap. Array(vector ) variable y Creating arrays >> x=[5 3 2 11 6 5 2 ] >> x= 7:10 x= >> x=rand(1,8). Array operations. x =[5 3 7 ],y=[1 2 3] Array scalar operations >>x+2 ans =[7 5 9]

neo
Download Presentation

COMP 116: Introduction to Scientific Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMP 116: Introduction to Scientific Programming Lecture 3: Arrays and Matrices

  2. Recap • Array(vector) variable y • Creating arrays >> x=[5 3 2 11 6 5 2] >> x= 7:10 x= >> x=rand(1,8)

  3. Array operations x=[5 3 7],y=[1 2 3] Array scalar operations >>x+2 ans=[7 5 9] >>x*2 Array vector operations (Both arrays are of same length) >>x+y >>x.*y (Use . for multiplication and division) ans=[5 6 21] Plotting (Both arrays are of same length) >>plot(x,y)

  4. For today • Arrays • Indexing • Matrices • Creating • Images

  5. Indexing arrays • x=[3 6 9 12 15 18] • x(i) gives you the ith element of the array >>x(2) >>x(3)=7 >>x(8) • x(end) the last element >>x(end) • x(i:j) array of elements in {i,i+1,i+2,…,j} positions >>x(3:5) • x([i j k l]) gives you the array [x(i) x(j) x(k) x(l)] >>x([5 2 1])

  6. Exercise • Create the array [3 4 5 6 7] using the colon operator and assign it to variable x >>x=3:7 • Modify this array to the array [3 9 10 11 7] >>x(2:4)=9:11

  7. For today • Arrays • Indexing • Matrices • Creating • Images

  8. Matrices • Each element of the matrix is a variable(memory location) • To create this 3x4 matrix • y=[3 4 8 41; 35 7 22 11; 8 11 27 2] • An array is a 1xn matrix

  9. Matrix operations • m=[3 6 4; 1 2 3] • Matrix-Scalar operations >>m=m*2 >>n=m-3 • Matrix-matrix operations (must be same size) >>p=m+n >>q=m.*n (use . for multiplication and division)

  10. Creating matrices >>rand(m,n) mxn matrix of random numbers >>zeros(m,n) all zeros >>ones(m,n) all ones >>eye(m,n) identity matrix >>diag(v) diagonal matrix >>doc <command_name> to get detailed documentation e.g. >>doc diag

  11. For today • Arrays • Indexing • Matrices • Creating • Images

  12. Images • Images are matrices where each element of the matrix corresponds to the intensity at the corresponding pixel >>im_matrix=imread(‘picture.jpg’) reads the file picture.jpg into the matrix im_matrix • doc imread for more information >>imagesc(im_matrix) displays the image corresponding to im_matrix

More Related