Presentation is loading. Please wait.

Presentation is loading. Please wait.

Numerical Computing with . MATLAB for Scientists and Engineers

Similar presentations


Presentation on theme: "Numerical Computing with . MATLAB for Scientists and Engineers"— Presentation transcript:

1 Numerical Computing with . MATLAB for Scientists and Engineers
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]' 1 2 3 1 2 3 8 4 5 6 4 5 6 8 7 8 9 7 8 9 8

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

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

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) 1 2 3 4 5 6 4 5 6 4 5 6 7 8 9 7 8 9 7 8 9 4 5 6 4 5 6 10 11 12 7 8 9 7 8 9

7 [r c] = ind2sub(size(a),10)
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 2 b=a(7) 8 2 6 10 7 8 9 3 [r c] = ind2sub(size(a),10) 3 7 11 10 11 12 4 r=2 c=3 4 8 12 1 2 3

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

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

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 5 5 6 3 7 4 2 8 8 8 6 8 8 6 6 5 5 8 3 7 sx=sort(a,2) 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 8 3 8 6 6 5 2 5 8 3 7 As = A(idx,:) 4 2 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 8 3 2 8 6 6 5 1 4 5 8 3 7 4 2 6 6 5 k=find(A>7); A(k)=0 5 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 find(a>3) 2 3 6 7 find(a>3,2) 2 3 find(a>3,2,'last') 6 7

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

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) i 1 3 1 1 2 3 3 min(min(a)) 1

17 Flipping and Rotating Matrices can be flipped, rotated and shifted.
flipud(a) fliplr(a) a 1 2 3 rot90(a) rot90(a,2) 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 a 5 5 9 9 1 2 3 4 5 6 1 2 3 1 7 8 9 5 6 4 5 9 7 8 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


Download ppt "Numerical Computing with . MATLAB for Scientists and Engineers"

Similar presentations


Ads by Google