handling arrays 2/2
DESCRIPTION
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, - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Handling Arrays 2/2](https://reader030.vdocuments.us/reader030/viewer/2022033106/5681316f550346895d97eacf/html5/thumbnails/1.jpg)
Handling Arrays 2/2
Numerical Computingwith .
MATLAB for Scientists and Engineers
![Page 2: Handling Arrays 2/2](https://reader030.vdocuments.us/reader030/viewer/2022033106/5681316f550346895d97eacf/html5/thumbnails/2.jpg)
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
![Page 3: Handling Arrays 2/2](https://reader030.vdocuments.us/reader030/viewer/2022033106/5681316f550346895d97eacf/html5/thumbnails/3.jpg)
3
Padding a Column
Inserting a column at a larger index than the size automatically fills in-betweens with ze-ros.
11 22 33
44 55 66
77 88 99
a= [1 2 3; 4 5 6; 7 8 9]a= [1 2 3; 4 5 6; 7 8 9] a(:,5)=[8 8 8]'a(:,5)=[8 8 8]'
11 22 33
44 55 66
77 88 99
00
00
00
88
88
88
![Page 4: Handling Arrays 2/2](https://reader030.vdocuments.us/reader030/viewer/2022033106/5681316f550346895d97eacf/html5/thumbnails/4.jpg)
4
Column Permutation
Use permuted array indexing
11 22 33
44 55 66
77 88 99
aa
112233
445566
778899
b=a(:,end:-1:1)b=a(:,end:-1:1)
1122 33
4455 66
7788 99
c=a(:,[2 3 1])c=a(:,[2 3 1])
![Page 5: Handling Arrays 2/2](https://reader030.vdocuments.us/reader030/viewer/2022033106/5681316f550346895d97eacf/html5/thumbnails/5.jpg)
5
Reshaping
Change matrix dimensions
11 22 33
44 55 66
77 88 99
aa b=reshape(a,2,6)b=reshape(a,2,6)
11 77 22
44 1010 55
9988
1111
33
66 1212
1010 1111 1212
b=reshape(a,3, [ ] )b=reshape(a,3, [ ] )
11 1010 88
44 22 1111
66
99
77 55 33 1212
![Page 6: Handling Arrays 2/2](https://reader030.vdocuments.us/reader030/viewer/2022033106/5681316f550346895d97eacf/html5/thumbnails/6.jpg)
6
Replicate Array
Use the given matrix as the tile in order to produce a larger matrix.
b = repmat (a(2:3,:), 2, 2)b = repmat (a(2:3,:), 2, 2)
11 22 33
44 55 66
77 88 99
aa
1010 1111 1212
44 55 66
77 88 99
44 55 66
77 88 99
44 55 66
77 88 99
44 55 66
77 88 99
![Page 7: Handling Arrays 2/2](https://reader030.vdocuments.us/reader030/viewer/2022033106/5681316f550346895d97eacf/html5/thumbnails/7.jpg)
7
Single Indexing
Looking at a matrix as a vector
sub2ind(size(a),3,2)sub2ind(size(a),3,2)11 22 33
44 55 66
77 88 99
aa
1010 1111 1212
77
b=a(7)b=a(7) 88
1
2
3
4
5
6
7
8
9
10
11
12
1
2
3
4
1 2 3
[r c] = ind2sub(size(a),10)[r c] = ind2sub(size(a),10)
r=2r=2 c=3c=3
![Page 8: Handling Arrays 2/2](https://reader030.vdocuments.us/reader030/viewer/2022033106/5681316f550346895d97eacf/html5/thumbnails/8.jpg)
8
abs(a) > 1abs(a) > 1
-3-3a=-3:3a=-3:3 -2-2 -1-1 00 11 22 33
11 11 00 00 00 11 11
a(abs(a) > 1)a(abs(a) > 1) -3-3 -2-2 22 33
Logical Values
a(logical([1010111]))a(logical([1010111])) -3-3 -1-1 22 3311
Logical Arrays
![Page 9: Handling Arrays 2/2](https://reader030.vdocuments.us/reader030/viewer/2022033106/5681316f550346895d97eacf/html5/thumbnails/9.jpg)
9
Scalar Expansion
Filling matrices with a single value
a=3a=3
33
b=a(ones(2,3))b=a(ones(2,3))
33 33
33 33 33
22
b(:) = 2b(:) = 2
22 22
22 22 22
11
b=true(2,3)b=true(2,3)
11 11
11 11 11
11
b(2,2:3)=falseb(2,2:3)=false
11 11
11 00 00
![Page 10: Handling Arrays 2/2](https://reader030.vdocuments.us/reader030/viewer/2022033106/5681316f550346895d97eacf/html5/thumbnails/10.jpg)
10
Sorting Arrays
Sort the elements in ascending or descend-ing order.
22x=randperm(6)x=randperm(6) 33 55 44 66 11
11xs = sort(x)xs = sort(x) 22 33 44 55 66
66xs = sort(x,'descend')xs = sort(x,'descend') 55 44 33 22 11
[xs,idx]=sort(x)[xs,idx]=sort(x)
11xsxs 22 33 44 55 66
66idxidx 11 22 44 33 55
![Page 11: Handling Arrays 2/2](https://reader030.vdocuments.us/reader030/viewer/2022033106/5681316f550346895d97eacf/html5/thumbnails/11.jpg)
11
Sorting Matrices
The default is the column-wise sorting.
44
a=floor(10*rand(3,4))a=floor(10*rand(3,4))
22 00 88
88 66 66 55
55 88 33 77
44 22 00 55
55 66 33 77
88 88 66 88
sx=sort(a)sx=sort(a)
00 22 44 88
55 66 66 88
33 55 77 88
sx=sort(a,2)sx=sort(a,2)
for row-wise sortingfor row-wise sorting
![Page 12: Handling Arrays 2/2](https://reader030.vdocuments.us/reader030/viewer/2022033106/5681316f550346895d97eacf/html5/thumbnails/12.jpg)
12
Sort Using Pivot
Get sorting index and apply permutation.
44
A=floor(10*rand(3,4))A=floor(10*rand(3,4))
22 00 88
88 66 66 55
55 88 33 77
11
33
22
[tmp,idx]=sort(A(:,3))[tmp,idx]=sort(A(:,3))
As = A(idx,:)As = A(idx,:)
idxidx
44 22 00 88
55 88 33 77
88 66 66 55
![Page 13: Handling Arrays 2/2](https://reader030.vdocuments.us/reader030/viewer/2022033106/5681316f550346895d97eacf/html5/thumbnails/13.jpg)
13
Sub-array Searching 1/2
Substituting all elements larger than 7 to 0
44
A=floor(10*rand(3,4))A=floor(10*rand(3,4))
22 00 88
88 66 66 55
55 88 33 77
11
22
44
[r,c] = find(A>7)[r,c] = find(A>7)
k=find(A>7); A(k)=0k=find(A>7); A(k)=0
cc
22
33
11
rr
44 22 00 00
00 66 66 55
55 00 33 77
![Page 14: Handling Arrays 2/2](https://reader030.vdocuments.us/reader030/viewer/2022033106/5681316f550346895d97eacf/html5/thumbnails/14.jpg)
14
Sub-array Searching 2/2
First two or last two meeting the condition
find(a>3)find(a>3)
33a=randperm(7)a=randperm(7) 44 66 22 11 77 55
22 33 66 77
find(a>3,2)find(a>3,2) 22 33
find(a>3,2,'last')find(a>3,2,'last') 66 77
![Page 15: Handling Arrays 2/2](https://reader030.vdocuments.us/reader030/viewer/2022033106/5681316f550346895d97eacf/html5/thumbnails/15.jpg)
15
Minimum and Maximum
The minimum (or maximum) value and the corresponding index
min(a)min(a)
33aa 11 66 22 11 77 55
11
find(a==m)find(a==m) 22 55
[m,i]=min(a)[m,i]=min(a) 11 22m i
max(a)max(a) 77
find(a==M)find(a==M) 66
[M,j]=max(a)[M,j]=max(a) 77 66M j
![Page 16: Handling Arrays 2/2](https://reader030.vdocuments.us/reader030/viewer/2022033106/5681316f550346895d97eacf/html5/thumbnails/16.jpg)
16
Min / Max of a Matrix
The default is column-wise operation.
a = randn(3,7);b = 10*a;c = floor(b)
a = randn(3,7);b = 10*a;c = floor(b)
11 88 44 66 33 55 88
[m,i]=min(a)[m,i]=min(a)11
11
m
i
min(min(a))min(min(a)) 11
66 88 88 88 22 77 55
33 55 88 66 33 33 33
55 44 66 22 33 33
33 11 11 22 33 33
![Page 17: Handling Arrays 2/2](https://reader030.vdocuments.us/reader030/viewer/2022033106/5681316f550346895d97eacf/html5/thumbnails/17.jpg)
17
Flipping and Rotating
Matrices can be flipped, rotated and shifted.
11 22 33
44 55 66
77 88 99
aaflipud(a)flipud(a) fliplr(a)fliplr(a)
rot90(a)rot90(a) rot90(a,2)rot90(a,2)
circshift(a,1)circshift(a,1) circshift(a,-1)circshift(a,-1)
circshift(a,[0 1])circshift(a,[0 1]) circshift(a,[0 -1])circshift(a,[0 -1])
![Page 18: Handling Arrays 2/2](https://reader030.vdocuments.us/reader030/viewer/2022033106/5681316f550346895d97eacf/html5/thumbnails/18.jpg)
18
Upper, Lower and Diagonal Matrix
Various matrices based on the original matrix
11 22 33
44 55 66
77 88 99
aa
d=diag(a)d=diag(a) 11
55
99
c=diag(d)c=diag(d) 11 00 00
00 55 00
00 00 99
11 22 33
00 55 66
00 00 99
11 00 00
44 55 00
77 88 99
U=triu(a)U=triu(a) U=tril(a)U=tril(a)
![Page 19: Handling Arrays 2/2](https://reader030.vdocuments.us/reader030/viewer/2022033106/5681316f550346895d97eacf/html5/thumbnails/19.jpg)
19
Grayscale Image 1/2
Gray scale image is a 2-D matrix value: 0~256 lovetriangle.mlovetriangle.m
M = imread('lovetriangle.jpg');figure(1), imshow(M);figure(2), imshow(M(5:88,215:335));S = M; % Add photo frameS(1:5,:) = 0; S(end-5:end,:) = 0;S(:,1:5) = 0; S(:,end-5:end) = 0;figure(3), imshow(S);
M = imread('lovetriangle.jpg');figure(1), imshow(M);figure(2), imshow(M(5:88,215:335));S = M; % Add photo frameS(1:5,:) = 0; S(end-5:end,:) = 0;S(:,1:5) = 0; S(:,end-5:end) = 0;figure(3), imshow(S);
Black photoframe
![Page 20: Handling Arrays 2/2](https://reader030.vdocuments.us/reader030/viewer/2022033106/5681316f550346895d97eacf/html5/thumbnails/20.jpg)
20
Grayscale Image 2/2
Matrix operations apply to grayscale imagesM fliplr(M) triu(M)
M([M<5])=250 circshift(M,[0 160]) flipud(M)
![Page 21: Handling Arrays 2/2](https://reader030.vdocuments.us/reader030/viewer/2022033106/5681316f550346895d97eacf/html5/thumbnails/21.jpg)
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.msquares.m
S = uint8(zeros(100,100,3));S( 1: 50, 1: 50,1) = 255; % redS( 1: 50,51:100,2) = 255; % greenS(51:100, 1: 50,3) = 255; % blueS(51:100,51:100,1:2) = 255; % yellowfigure(1), imshow(S);
S = uint8(zeros(100,100,3));S( 1: 50, 1: 50,1) = 255; % redS( 1: 50,51:100,2) = 255; % greenS(51:100, 1: 50,3) = 255; % blueS(51:100,51:100,1:2) = 255; % yellowfigure(1), imshow(S);
![Page 22: Handling Arrays 2/2](https://reader030.vdocuments.us/reader030/viewer/2022033106/5681316f550346895d97eacf/html5/thumbnails/22.jpg)
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.
![Page 23: Handling Arrays 2/2](https://reader030.vdocuments.us/reader030/viewer/2022033106/5681316f550346895d97eacf/html5/thumbnails/23.jpg)
23
Solution 1
Script and Screenshotphoto_frame.mphoto_frame.m
![Page 24: Handling Arrays 2/2](https://reader030.vdocuments.us/reader030/viewer/2022033106/5681316f550346895d97eacf/html5/thumbnails/24.jpg)
24
Exercise 2 – Photo Puzzle
Cut a photo into 2x2 pieces and put them in a random position.
![Page 25: Handling Arrays 2/2](https://reader030.vdocuments.us/reader030/viewer/2022033106/5681316f550346895d97eacf/html5/thumbnails/25.jpg)
25
Solution 2
Script and Screenshotpuzzle2x2.mpuzzle2x2.m