1 / 25

Handling Arrays 2/2

Handling Arrays 2/2. Numerical Computing with . MATLAB for Scientists and Engineers. You will be able to. Add, delete, permute all or sum of the vectors and matrices , Sort and search matrix elements, Change the shape of the matrix,

bo-mejia
Download Presentation

Handling Arrays 2/2

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. Handling Arrays 2/2 Numerical Computing with . MATLAB for Scientists and Engineers

  2. You will be able to • Add, delete, permute all or sum of the vectors and matrices, • Sort and search matrix elements, • Change the shape of the matrix, • Flip, Rotate and Shift the matrix, • Manipulate 2D images

  3. Padding a Column • Inserting a column at a larger index than the size automatically fills in-betweens with zeros. a= [1 2 3; 4 5 6; 7 8 9] a(:,5)=[8 8 8]' 8 0 1 2 3 1 2 3 8 0 4 5 6 4 5 6 8 0 7 8 9 7 8 9

  4. Column Permutation • Use permuted array indexing a b=a(:,end:-1:1) c=a(:,[2 3 1]) 1 2 3 1 1 2 3 3 2 4 5 6 4 4 5 6 6 5 7 8 9 7 7 8 9 9 8

  5. Reshaping • Change matrix dimensions b=reshape(a,2,6) a 3 8 1 7 2 9 1 2 3 6 11 4 10 5 12 4 5 6 b=reshape(a,3, [ ] ) 7 8 9 6 1 10 8 10 11 12 9 4 2 11 12 7 5 3

  6. Replicate Array • Use the given matrix as the tile in order to produce a larger matrix. a b = repmat(a(2:3,:), 2, 2) 4 5 6 4 5 6 1 2 3 7 8 9 7 8 9 4 5 6 7 8 9 4 5 6 4 5 6 7 8 9 7 8 9 10 11 12

  7. Single Indexing • Looking at a matrix as a vector a sub2ind(size(a),3,2) 7 1 2 3 1 1 5 9 4 5 6 b=a(7) 8 2 6 2 10 7 8 9 3 [r c] = ind2sub(size(a),10) 7 3 11 10 11 12 4 r=2 c=3 8 4 12 1 2 3

  8. Logical Arrays a=-3:3 -3 -2 -1 0 1 2 3 1 1 0 0 0 1 1 abs(a) > 1 Logical Values -3 -2 2 3 a(abs(a) > 1) 1 -3 -1 2 3 a(logical([1010111]))

  9. Scalar Expansion • Filling matrices with a single value a=3 b=a(ones(2,3)) b(:) = 2 3 3 3 2 2 2 3 3 3 2 2 2 b=true(2,3) b(2,2:3)=false 1 1 1 1 1 1 1 1 1 1 0 0

  10. Sorting Arrays • Sort the elements in ascending or descending order. x=randperm(6) 2 3 5 4 6 1 xs = sort(x) 1 2 3 4 5 6 xs = sort(x,'descend') 6 5 4 3 2 1 [xs,idx]=sort(x) xs 1 2 3 4 5 6 idx 6 1 2 4 3 5

  11. Sorting Matrices • The default is the column-wise sorting. a=floor(10*rand(3,4)) sx=sort(a) 4 2 0 5 5 6 3 7 4 2 0 8 8 8 6 8 8 6 6 5 5 8 3 7 sx=sort(a,2) 0 2 4 8 5 6 6 8 for row-wise sorting 3 5 7 8

  12. Sort Using Pivot • Get sorting index and apply permutation. [tmp,idx]=sort(A(:,3)) A=floor(10*rand(3,4)) idx 1 4 2 0 8 3 8 6 6 5 2 5 8 3 7 As = A(idx,:) 4 2 0 8 5 8 3 7 8 6 6 5

  13. Sub-array Searching 1/2 • Substituting all elements larger than 7 to 0 [r,c] = find(A>7) r c A=floor(10*rand(3,4)) 2 1 4 2 0 8 3 2 8 6 6 5 1 4 5 8 3 7 4 2 0 0 0 6 6 5 k=find(A>7); A(k)=0 5 0 3 7

  14. Sub-array Searching 2/2 • First two or last two meeting the condition a=randperm(7) 3 4 6 2 1 7 5 2 3 6 7 find(a>3) 2 3 find(a>3,2) 6 7 find(a>3,2,'last')

  15. Minimum and Maximum • The minimum (or maximum) value and the corresponding index a 3 1 6 2 1 7 5 1 min(a) 7 max(a) 1 2 [m,i]=min(a) 7 6 [M,j]=max(a) m i M j 2 5 find(a==m) 6 find(a==M)

  16. Min / Max of a Matrix • The default is column-wise operation. a = randn(3,7); b = 10*a; c = floor(b) 1 8 4 6 3 5 8 6 8 8 8 2 7 5 3 5 8 6 3 3 3 m 1 5 4 6 2 3 3 [m,i]=min(a) 1 3 1 1 2 3 3 i 1 min(min(a))

  17. Flipping and Rotating • Matrices can be flipped, rotated and shifted. flipud(a) fliplr(a) a rot90(a) rot90(a,2) 1 2 3 4 5 6 circshift(a,1) circshift(a,-1) 7 8 9 circshift(a,[0 1]) circshift(a,[0 -1])

  18. Upper, Lower and Diagonal Matrix • Various matrices based on the original matrix d=diag(a) c=diag(d) 1 1 0 0 5 0 5 0 a 9 0 0 9 1 2 3 4 5 6 1 0 0 1 2 3 7 8 9 4 5 0 0 5 6 7 8 9 0 0 9 U=triu(a) U=tril(a)

  19. Grayscale Image 1/2 • Gray scale image is a 2-D matrix • value: 0~256 lovetriangle.m M = imread('lovetriangle.jpg'); figure(1), imshow(M); figure(2), imshow(M(5:88,215:335)); S = M; % Add photo frame S(1:5,:) = 0; S(end-5:end,:) = 0; S(:,1:5) = 0; S(:,end-5:end) = 0; figure(3), imshow(S); Black photo frame

  20. Grayscale Image 2/2 • Matrix operations apply to grayscale images M fliplr(M) triu(M) M([M<5])=250 circshift(M,[0 160]) flipud(M)

  21. Color Image • Color image is a 3-D matrix • 2x2 matrix of pixels • Each pixel is an [R G B] array • R, G, B values: 0~255 squares.m S = uint8(zeros(100,100,3)); S( 1: 50, 1: 50,1) = 255; % red S( 1: 50,51:100,2) = 255; % green S(51:100, 1: 50,3) = 255; % blue S(51:100,51:100,1:2) = 255; % yellow figure(1), imshow(S);

  22. Exercise 1 – Color Photo Frame • Choose any photo you like and add color photo frame outside of the photo. You may choose the color of the photo frame as you like.

  23. Solution 1 • Script and Screenshot photo_frame.m

  24. Exercise 2 – Photo Puzzle • Cut a photo into 2x2 pieces and put them in a random position.

  25. Solution 2 • Script and Screenshot puzzle2x2.m

More Related