Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hackassin
GitHub Repository: hackassin/Coursera-Machine-Learning
Path: blob/master/Week 2/Octave Tutorial/3. Computing On Data/computing_on_data.txt
625 views
1
octave:5> A = [1 2; 3 4; 5 6]
2
A =
3
4
1 2
5
3 4
6
5 6
7
8
octave:6> B = [10 11; 12 13; 14 15]
9
B =
10
11
10 11
12
12 13
13
14 15
14
15
octave:7> C = [1 2 3; 4 5 6]
16
C =
17
18
1 2 3
19
4 5 6
20
21
octave:8> A * C
22
ans =
23
24
9 12 15
25
19 26 33
26
29 40 51
27
28
octave:9> B * C
29
ans =
30
31
54 75 96
32
64 89 114
33
74 103 132
34
35
octave:10> A .* B
36
ans =
37
38
10 22
39
36 52
40
70 90
41
42
octave:11> V = [1; 2; 3]
43
V =
44
45
1
46
2
47
3
48
49
octave:12> V + 1
50
ans =
51
52
2
53
3
54
4
55
56
octave:13> V' #transpose
57
ans =
58
59
1 2 3
60
61
octave:14> (V+1)'
62
ans =
63
64
2 3 4
65
66
octave:15> x = (V+1)'
67
x =
68
69
2 3 4
70
71
octave:16> max (x)
72
ans = 4
73
octave:17> [val, ind] = max (x)
74
val = 4
75
ind = 3
76
octave:18> A
77
A =
78
79
1 2
80
3 4
81
5 6
82
83
octave:19> max (A) #returns the column with the max value
84
ans =
85
86
5 6
87
88
octave:20> x
89
x =
90
91
2 3 4
92
93
octave:21> x < 3
94
ans =
95
96
1 0 0
97
98
octave:22> find (x<3)
99
ans = 1
100
octave:23> A
101
A =
102
103
1 2
104
3 4
105
5 6
106
107
octave:24> [r, c] = find(A>=7)
108
r = [](0x1)
109
c = [](0x1)
110
octave:25> [r, c] = find(A>=3)
111
r =
112
113
2
114
3
115
2
116
3
117
118
c =
119
120
1
121
1
122
2
123
2
124
125
octave:26> D = rand(3,1)
126
D =
127
128
0.037138
129
0.040905
130
0.339827
131
132
octave:27> D = rand(1,1)
133
D = 0.63497
134
octave:28> D = rand(1,0)
135
D = [](1x0)
136
octave:29> D = rand(1,3)
137
D =
138
139
0.39812 0.37656 0.65296
140
141
octave:30> floor(D)
142
ans =
143
144
0 0 0
145
146
octave:31> ceil(D)
147
ans =
148
149
1 1 1
150
151
octave:32> max (2,1)
152
ans = 2
153
octave:33> max (rand(2), rand(2))
154
ans =
155
156
0.51577 0.71198
157
0.19302 0.56167
158
159
octave:34> rand(2)
160
ans =
161
162
0.941753 0.428214
163
0.084452 0.279988
164
165
octave:35> #that was the max of two random matrices of 2x2 dimensions each
166
octave:35> A = magic(3)
167
A =
168
169
8 1 6
170
3 5 7
171
4 9 2
172
173
octave:36> max(A, [], 1)
174
ans =
175
176
8 9 7
177
178
octave:37> max(A, [], 2) #will return the max of each row
179
ans =
180
181
8
182
7
183
9
184
185
octave:38> max(A, [], 3)
186
ans =
187
188
8 1 6
189
3 5 7
190
4 9 2
191
192
octave:39> max(A, [], 4)
193
ans =
194
195
8 1 6
196
3 5 7
197
4 9 2
198
199
octave:40> A = magic (9)
200
A =
201
202
47 58 69 80 1 12 23 34 45
203
57 68 79 9 11 22 33 44 46
204
67 78 8 10 21 32 43 54 56
205
77 7 18 20 31 42 53 55 66
206
6 17 19 30 41 52 63 65 76
207
16 27 29 40 51 62 64 75 5
208
26 28 39 50 61 72 74 4 15
209
36 38 49 60 71 73 3 14 25
210
37 48 59 70 81 2 13 24 35
211
212
octave:41> sum(A,1)
213
ans =
214
215
369 369 369 369 369 369 369 369 369
216
217
octave:42> help sum
218
'sum' is a built-in function from the file libinterp/corefcn/data.cc
219
220
-- sum (X)
221
-- sum (X, DIM)
222
-- sum (..., "native")
223
-- sum (..., "double")
224
-- sum (..., "extra")
225
Sum of elements along dimension DIM.
226
227
If DIM is omitted, it defaults to the first non-singleton
228
dimension.
229
230
The optional "type" input determines the class of the variable used
231
for calculations. By default, operations on floating point inputs
232
(double or single) are performed in their native data type, while
233
operations on integer, logical, and character data types are
234
performed using doubles. If the argument "native" is given, then
235
the operation is performed in the same type as the original
236
argument.
237
238
For example:
239
240
sum ([true, true])
241
=> 2
242
sum ([true, true], "native")
243
=> true
244
245
If "double" is given the sum is performed in double precision even
246
for single precision inputs.
247
248
For double precision inputs, the "extra" option will use a more
249
accurate algorithm than straightforward summation. For single
250
precision inputs, "extra" is the same as "double". For all other
251
data type "extra" has no effect.
252
253
See also: cumsum, sumsq, prod.
254
255
Additional help for built-in functions and operators is
256
available in the online version of the manual. Use the command
257
'doc <topic>' to search the manual index.
258
259
Help and information about Octave is also available on the WWW
260
at https://www.octave.org and via the [email protected]
261
mailing list.
262
octave:43> A
263
A =
264
265
47 58 69 80 1 12 23 34 45
266
57 68 79 9 11 22 33 44 46
267
67 78 8 10 21 32 43 54 56
268
77 7 18 20 31 42 53 55 66
269
6 17 19 30 41 52 63 65 76
270
16 27 29 40 51 62 64 75 5
271
26 28 39 50 61 72 74 4 15
272
36 38 49 60 71 73 3 14 25
273
37 48 59 70 81 2 13 24 35
274
275
octave:44> sum(A)
276
ans =
277
278
369 369 369 369 369 369 369 369 369
279
280
octave:45> B = [1 2; 3 4; 5 6]
281
B =
282
283
1 2
284
3 4
285
5 6
286
287
octave:46> sum(B)
288
ans =
289
290
9 12
291
292
octave:47> sum(B,1)
293
ans =
294
295
9 12
296
297
octave:48> sum(B,2)
298
ans =
299
300
3
301
7
302
11
303