Attachment 'OlafMatrixAlgebraScript.m'
Download 1 %% Vector basics
2
3 % Row vector
4 x = [1 1 1 1]
5
6 % Column vector
7 y = [1 1 1 1]'
8
9 % create vector with sequence of numbers
10 z = [1:2:10]
11 z = [-1:0.01:1]
12
13 % Getting an element out of a vector
14 z(2)
15 z(2:3)
16
17 % looping over elements
18 for i = 1:4,
19 display( x(i) );
20 end;
21
22 % Add or multiply values etc.
23 x + 10
24 2*x
25
26
27 %% Vector multiplication
28 % Transpose vectors
29 x
30 x'
31
32 % Vector multiplication: row vector * column vector (for time being)
33 x,y % just for display
34 x*y
35
36 % Common error: multiply vectors of different lengths:
37 z = [1 1 1]'
38 x*z
39
40 % Define two parallel vectors:
41 x = [1 0 1 0]
42 y = [1 0 1 0]'
43
44 % Multiply vectors
45 x*y
46
47 % Define two orthogonal vectors:
48 x = [1 0 1 0]
49 y = [0 1 0 1]'
50
51 % Multiply vectors
52 x*y
53
54
55 %% Some things you can do with vectors
56 % Define a vector
57 x = [1 2 3 4]
58
59 % Define another vector
60 y = [0 0 1 0]' % note the transpose
61
62 % Multiply the vectors
63 x*y
64
65 % Define another vector
66 z = [1 1 1 1]'
67
68 % Sum of elements of x:
69 x*z
70
71 % Average of elements of x
72 % length(x) yields the number of elements of a vector
73 x*z / length(x)
74
75 % Define another vector
76 y = [1 -1 1 -1]'
77
78 % Subtract sum of even from sum of odd elements in x:
79 x*y
80
81 clear
82 %% Matrices
83
84 % Define a (diagonal) matrix
85 x = [1 1 1; 2 2 2]
86
87 % Getting elements out of matrices
88 x(1,2)
89 x(:,1)
90 x(2,:)
91
92 % Getting matrix dimensions
93 size(x) % returns number of rows and columns, respectively
94 [m,n] = size(x)
95
96 %% Matrices c'd
97 % Matrix transpose
98 x
99 y = x'
100
101 % adding/multiplying values etc.
102 x + 2
103 2 * x
104
105 % Define another matrix
106 z = [1 2 3; 4 5 6]
107
108 % Subtract z from x
109 x - z
110
111 % Add z to x
112 x + z
113
114 % Elementwise multiplication (NOT "matrix multiplication": "*")
115 x .* z
116
117 % Try... (matrix multiplication)
118 x * z
119
120 % Elementwise division (NOT "matrix division/inversion": "/")
121 x ./ z
122
123 % Try... (matrix inversion)
124 x / z
125
126 %% Multiplying matrix and vector
127 % Define a vector
128 v = [1 1 1]'
129
130 % Mutliply matrix x with vector v (and obtain sum within each row of x)
131 x
132 v
133 x * v
134
135 % matrix and vector dimensions must agree
136 v * x % does not work
137
138 %% interpretation as sum of column vectors
139 % x * v results in the sum of column vectors in x
140 x * v
141 1*x(:,1) + 1*x(:,2) + 1*x(:,3)
142
143 %% Matrix multiplication
144 % Multiply two matrices
145 x * y
146
147 % Multiply two matrices
148 y * x
149
150 % Note: x*y is not necessarily y*x!!!
151
152 % Define new matrix
153 z = [1 1; 2 2]
154
155 % Works:
156 y*z
157 z*x
158
159 % Doesn't work:
160 z*y
161 x*z
162
163 %% Make your own matrices
164
165 % Identity matrix
166 id = eye(3)
167
168 % Example:
169 x*id
170
171 id*x'
172
173 % diagonal matrix
174 di = eye(3)
175 di(1,1) = 1
176 di(2,2) = 2
177 di(3,3) = 3
178
179 % Multiplies columns by a factor:
180 x*di
181
182 % Multiplies rows by a factor
183 di*y
184
185 %% Some useful functions
186
187 % sum/average across columns of a matrix
188 % note: most Matlab function operate column-by-column
189 sum( x )
190 mean( x )
191
192 % sum across rows of a matrix
193 sum( x' )
194 mean( x' )
195
196 % get the diagonal of a (square) matrix
197 mydiag = diag(di)
198
199 % dimensions of a matrix, size()
200 [m,n] = size(x)
201
202 % dimension of a vector
203 diaglen = length(mydiag)
204
205 % create a matrix/vector with ones
206 ones(10) % creates 10x10 matrix
207 ones(10,1) % creates 10x1 column vector
208
209 % create a matrix/vector with zeros
210 zeros(10) % creates 10x10 matrix
211 zeros(1,10) % creates 1x10 row vector
212
213 % create matrix/vector with standard uniform random numbers in interval 0-1
214 rand(3)
215
216 % create matrix/vector with standard normal random numbers in interval 0-1
217 randn(3)
218
219 % "reshaping" a matrix or a vector
220 x = 1:10
221 reshape(x,5,2)
222 reshape(x,2,5)
223
224 % Kronecker product ("concatenates" matrices/vectors)
225 kron(id,y)
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.