Programming Languages Matlab 3101 - Class 3
3/26/08, Instructor: Blake Shaw
Contents
- Notes: HW1 grades, Quiz,
- Review
- Today: focus on data structures
- Numbers, Lists, and Matrices
- Creating special matrices
- Matrices can be high-dimensional
- circshift
- Matrices can be sparse
- Creating indexes
- Avoiding loops
- Strings
- basic types: double, char unit8
- Cells
- Saving and Loading variables
- User Input
- About HW3
- Quiz review
Notes: HW1 grades, Quiz,
HW 1, avg is 82.5, std deviation is 14, quiz is next week, it will be the second half of class, 1 hour, closed book, closed computer, closed everything.
Review
Loops, and Control flow: if, while, for, try/catch. Displaying text with formatting. More plotting: spy, imagesc.
Today: focus on data structures
matrices (including numbers, and lists), strings, cells, saving and loading
Numbers, Lists, and Matrices
show some examples with [], cat, indexing, transpose, flip, rot90, reshape, flattening with (:)
a = 34; A = [4, 5; 6, 8; 10, 15] B = [7, 8; 10, 12] C = [A; B; [9, 8]] C = [A', B] D = [A(1:2, 1:2), B]
A =
4 5
6 8
10 15
B =
7 8
10 12
C =
4 5
6 8
10 15
7 8
10 12
9 8
C =
4 6 10 7 8
5 8 15 10 12
D =
4 5 7 8
6 8 10 12
A = round(10*rand(3, 2)) B = round(10*rand(3, 2)) C = round(10*rand(3, 2)) D = cat(3, A, B, C); size(D)
A =
3 2
3 10
4 7
B =
6 4
7 1
2 6
C =
2 8
3 5
4 3
ans =
3 2 3
A = round(20*rand(3, 3)); fliplr(A) flipud(A) A rot90(A) sum(sum(sum(D))) sum(D(:)) flatD = D(:); reshape(flatD, 3, 6)
ans =
1 6 15
1 7 6
0 4 5
ans =
5 4 0
6 7 1
15 6 1
A =
15 6 1
6 7 1
5 4 0
ans =
1 1 0
6 7 4
15 6 5
ans =
80
ans =
80
ans =
3 2 6 4 2 8
3 10 7 1 3 5
4 7 2 6 4 3
Creating special matrices
zeroes, ones, eye, diag, linspace, :, rand, randn
A = zeros(3, 2, 4) A = ones(3, 3) A = eye(3, 3) A = diag(eye(3, 3)) A = diag([2, 3, 5, 7]) b = linspace(-pi, pi, 1000); length(b) plot(b, sin(b)) a = 6:0.2:9; length(a)
A(:,:,1) =
0 0
0 0
0 0
A(:,:,2) =
0 0
0 0
0 0
A(:,:,3) =
0 0
0 0
0 0
A(:,:,4) =
0 0
0 0
0 0
A =
1 1 1
1 1 1
1 1 1
A =
1 0 0
0 1 0
0 0 1
A =
1
1
1
A =
2 0 0 0
0 3 0 0
0 0 5 0
0 0 0 7
ans =
1000
ans =
16
a = rand(10000, 1); b = randn(10000, 1); hist(a) hist(b)
Matrices can be high-dimensional
squeeze, shiftdim
D = floor(10*rand(3, 2, 3)) D2 = sum(D, 2) size(D2) D3 = squeeze(D2) size(D3)
D(:,:,1) =
4 7
1 5
4 3
D(:,:,2) =
5 1
3 6
8 3
D(:,:,3) =
9 5
1 9
4 5
D2(:,:,1) =
11
6
7
D2(:,:,2) =
6
9
11
D2(:,:,3) =
14
10
9
ans =
3 1 3
D3 =
11 6 14
6 9 10
7 11 9
ans =
3 3
D size(D) D4 = shiftdim(D, 3) size(D4)
D(:,:,1) =
4 7
1 5
4 3
D(:,:,2) =
5 1
3 6
8 3
D(:,:,3) =
9 5
1 9
4 5
ans =
3 2 3
D4(:,:,1) =
4 7
1 5
4 3
D4(:,:,2) =
5 1
3 6
8 3
D4(:,:,3) =
9 5
1 9
4 5
ans =
3 2 3
circshift
A = floor(10*rand(5, 5))
A =
2 8 3 0 7
5 0 4 2 1
7 0 8 7 1
7 7 0 7 0
7 7 6 0 8
A circshift(A, 1) circshift(A, [1, 1])
A =
2 8 3 0 7
5 0 4 2 1
7 0 8 7 1
7 7 0 7 0
7 7 6 0 8
ans =
7 7 6 0 8
2 8 3 0 7
5 0 4 2 1
7 0 8 7 1
7 7 0 7 0
ans =
8 7 7 6 0
7 2 8 3 0
1 5 0 4 2
1 7 0 8 7
0 7 7 0 7
Matrices can be sparse
sparse, nnz, nonzeros, sprand
N = 10^3; A = zeros(N, N); size(A)
ans =
1000 1000
N = 10^5; B = sparse(N, N); size(B)
ans =
100000 100000
B(1, 5) = 9; B(4, 9) = 12; B = B .^ 2; B(1, 5)
ans =
81
nnz(B) nonzeros(B) C = sprand(N, N, 0.0005);
ans =
2
ans =
81
144
nnz(C)
imagesc(C(1:100, 1:100)); axis image;
ans =
4998683
Creating indexes
:, randperm, sub2ind, ind2sub
X = rand(100,1); N = length(X); i = randperm(N); X(i(1:10)) [i, j] = ind2sub([5, 5], 14)
ans =
0.3139
0.2701
0.4732
0.2856
0.1056
0.1234
0.6130
0.9145
0.4224
0.1527
i =
4
j =
3
Avoiding loops
repmat, find, circshift, relational operators, sort, permute
a = rand(10, 10);
[i, j] = find(a < 0.5) [i, j]
i =
1
3
4
5
6
8
9
1
2
3
5
7
8
10
1
2
3
5
7
9
2
5
7
10
3
6
8
1
2
3
4
6
7
8
10
1
2
3
8
1
7
10
2
3
4
6
7
9
10
2
4
5
7
10
j =
1
1
1
1
1
1
1
2
2
2
2
2
2
2
3
3
3
3
3
3
4
4
4
4
5
5
5
6
6
6
6
6
6
6
6
7
7
7
7
8
8
8
9
9
9
9
9
9
9
10
10
10
10
10
ans =
1 1
3 1
4 1
5 1
6 1
8 1
9 1
1 2
2 2
3 2
5 2
7 2
8 2
10 2
1 3
2 3
3 3
5 3
7 3
9 3
2 4
5 4
7 4
10 4
3 5
6 5
8 5
1 6
2 6
3 6
4 6
6 6
7 6
8 6
10 6
1 7
2 7
3 7
8 7
1 8
7 8
10 8
2 9
3 9
4 9
6 9
7 9
9 9
10 9
2 10
4 10
5 10
7 10
10 10
a = [1, 2, 3] a([2, 1, 3])
a =
1 2 3
ans =
2 1 3
X = rand(100, 2); p = rand(1, 2);
figure(1); clf; hold on; scatter(X(:, 1), X(:, 2)) scatter(p(1, 1), p(1, 2), 'r+') hold off;
dists = sum((X - repmat(p, 100, 1)).^2, 2); i = find(dists < 0.01) [C, I] = min(dists)
i =
1
8
25
27
30
39
C =
0.0010
I =
39
Strings
declaring, concatenating, indexing, converting, strfind, strtok
a = 'hello'; length(a) b = [a, ' blake']; strfind(b, 'll') strfind(b, 'afdas') %c = ['string1'; 'this is string 2'; 'hi']
ans =
5
ans =
3
ans =
[]
basic types: double, char unit8
char(double(b) + 1) b(5) b(2:5)
ans = ifmmp!cmblf ans = o ans = ello
Cells
declaring, concatenating
A = cell(4, 1);
A{1} = 'this is a string';
A{2} = rand(10, 10);
A{3} = 'this is another string';
A{4} = cell(2, 2);
B = cell(2, 2)
C = cell(2, 2)
D = {B, C}
D = {rand(10), 10, 'hello'}
D = [B C]
B =
[] []
[] []
C =
[] []
[] []
D =
{2x2 cell} {2x2 cell}
D =
[10x10 double] [10] 'hello'
D =
[] [] [] []
[] [] [] []
Saving and Loading variables
who, whos, clear, save, load
whos save results.mat clear whos whos -file results.mat load results.mat
Name Size Bytes Class Attributes A 4x1 1132 cell B 2x2 16 cell C 2x2 16 cell D 2x4 32 cell D2 3x1x3 72 double D3 3x3 72 double D4 3x2x3 144 double I 1x1 8 double N 1x1 8 double X 100x2 1600 double a 1x5 10 char ans 1x4 8 char b 1x11 22 char c 1x25 50 char dists 100x1 800 double flatD 18x1 144 double i 6x1 48 double j 54x1 432 double p 1x2 16 double s 1x2 4 char Name Size Bytes Class Attributes A 4x1 1132 cell B 2x2 16 cell C 2x2 16 cell D 2x4 32 cell D2 3x1x3 72 double D3 3x3 72 double D4 3x2x3 144 double I 1x1 8 double N 1x1 8 double X 100x2 1600 double a 1x5 10 char ans 1x4 8 char b 1x11 22 char c 1x25 50 char dists 100x1 800 double flatD 18x1 144 double i 6x1 48 double j 54x1 432 double p 1x2 16 double s 1x2 4 char
User Input
input, str2num
s = input('what is the temp today?\n', 's');
Error using ==> input Cannot call INPUT from EVALC.
str2num(s) + 1
About HW3
Talk about rot13 encoding, and the caesar cipher
Quiz review
%{ Class Notes 1:3 Working with matrices: http://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access/helpdesk/help/techdoc/math/f4-988203.html Basic Flow Control: http://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access/helpdesk/help/techdoc/learn_matlab/f4-1931.html %}