annotate examples.md @ 50:1810308dba49 kai

Moved install to download for backward compatibility.
author Kai T. Ohlhus <k.ohlhus@gmail.com>
date Wed, 28 Sep 2016 11:30:27 +0200
parents 171ca967fcc9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
23
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
1 ---
46
171ca967fcc9 Start new development branch closer to the current website,
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 43
diff changeset
2 layout: default
23
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
3 title: Examples
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
4 menu: true
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
5 ---
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
6
46
171ca967fcc9 Start new development branch closer to the current website,
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 43
diff changeset
7 ## Using Octave
23
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
8
46
171ca967fcc9 Start new development branch closer to the current website,
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 43
diff changeset
9 First, follow the [installation guide](install.html) to install Octave on your system. Then, launch the interactive prompt by typing `octave` in a terminal or by clicking the icon in the programs menu. For further guidance, see the manual page on [Running Octave]({{site.docs_url}}Running-Octave.html).
23
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
10
46
171ca967fcc9 Start new development branch closer to the current website,
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 43
diff changeset
11
171ca967fcc9 Start new development branch closer to the current website,
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 43
diff changeset
12 ## Language Basics
23
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
13
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
14 Click the headings below to see a manual page related to each topic
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
15 {: .panel}
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
16
46
171ca967fcc9 Start new development branch closer to the current website,
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 43
diff changeset
17
171ca967fcc9 Start new development branch closer to the current website,
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 43
diff changeset
18 ### [Variable Assignment]({{site.docs_url}}Variables.html)
23
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
19
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
20 Assign values to variables with `=` (Note: assignment is <a href="#" data-reveal-id="assignmentModal">pass-by-value</a>)
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
21
46
171ca967fcc9 Start new development branch closer to the current website,
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 43
diff changeset
22
171ca967fcc9 Start new development branch closer to the current website,
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 43
diff changeset
23 ### [Comments]({{site.docs_url}}Comments.html)
23
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
24
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
25 `%` starts a comment block that continues to the end of the line.
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
26
46
171ca967fcc9 Start new development branch closer to the current website,
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 43
diff changeset
27
171ca967fcc9 Start new development branch closer to the current website,
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 43
diff changeset
28 ### [Evaluation]({{site.docs_url}}Simple-Examples.html)
23
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
29
46
171ca967fcc9 Start new development branch closer to the current website,
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 43
diff changeset
30 The output of every command is printed to the console unless terminated with `;`.
171ca967fcc9 Start new development branch closer to the current website,
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 43
diff changeset
31 The `disp()` command can be used to print output anywhere. Use `exit` to quit the console.
23
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
32
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
33 {% highlight matlab %}
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
34 t = 99 + 1 % prints 't = 100'
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
35 t = 99 + 1; % nothing is printed
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
36 disp(t);
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
37 100
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
38 {% endhighlight %}
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
39
46
171ca967fcc9 Start new development branch closer to the current website,
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 43
diff changeset
40
171ca967fcc9 Start new development branch closer to the current website,
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 43
diff changeset
41 ### [Math]({{site.docs_url}}Arithmetic.html)
23
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
42
43
a1cb50997055 Reviewed and reformatted some files.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 30
diff changeset
43 Many mathematical operators are available in addition to the standard arithmetic.
a1cb50997055 Reviewed and reformatted some files.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 30
diff changeset
44 Operations are floating-point.
23
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
45 {% highlight matlab %}
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
46 x = 3/4*pi;
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
47 y = sin(x)
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
48 0.70711
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
49 {% endhighlight %}
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
50
46
171ca967fcc9 Start new development branch closer to the current website,
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 43
diff changeset
51
171ca967fcc9 Start new development branch closer to the current website,
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 43
diff changeset
52 ### [Matrices]({{site.docs_url}}Matrices.html)
23
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
53
43
a1cb50997055 Reviewed and reformatted some files.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 30
diff changeset
54 Arrays in Octave are called matrices.
a1cb50997055 Reviewed and reformatted some files.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 30
diff changeset
55 One-dimensional matrices are referred to as vectors.
a1cb50997055 Reviewed and reformatted some files.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 30
diff changeset
56 Use `space` or `,` to separate elements and `;` to start a new row.
23
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
57
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
58 {% highlight matlab %}
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
59 rowVec = [8 6 4]
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
60 8 6 4
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
61 columnVec = [8; 6; 4]
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
62 8
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
63 6
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
64 4
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
65 mat = [8 6 4; 2 0 -2]
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
66 8 6 4
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
67 2 0 -2
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
68 size(mat)
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
69 2 3
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
70 length(rowVec)
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
71 3
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
72 {% endhighlight %}
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
73
46
171ca967fcc9 Start new development branch closer to the current website,
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 43
diff changeset
74
171ca967fcc9 Start new development branch closer to the current website,
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 43
diff changeset
75 ### [Linear Algebra]({{site.docs_url}}Linear-Algebra.html)
23
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
76
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
77 Many common linear algebra operations are simple to program using Octave's matrix syntax.
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
78
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
79 {% highlight matlab %}
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
80 columnVec * rowVec
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
81 64 48 32
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
82 48 36 24
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
83 32 24 16
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
84 rowVec * columnVec
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
85 116
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
86 columnVec'
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
87 8 6 4
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
88 {% endhighlight %}
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
89
46
171ca967fcc9 Start new development branch closer to the current website,
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 43
diff changeset
90
171ca967fcc9 Start new development branch closer to the current website,
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 43
diff changeset
91 ### [Accessing Elements]({{site.docs_url}}Index-Expressions.html)
23
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
92
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
93 Octave is 1-indexed. Matrix elements are accessed as `matrix(rowNum, columnNum)`.
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
94
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
95 {% highlight matlab %}
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
96 mat(2,3)
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
97 -2
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
98 {% endhighlight %}
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
99
46
171ca967fcc9 Start new development branch closer to the current website,
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 43
diff changeset
100
171ca967fcc9 Start new development branch closer to the current website,
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 43
diff changeset
101 ### [Iteration]({{site.docs_url}}Statements.html)
23
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
102
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
103 Octave supports `for` and `while` loops, as well as other control flow structures.
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
104
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
105 {% highlight matlab %}
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
106 x = zeros(50,1);
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
107 for i=1:2:100 % iterate from 1 to 100 with step size 2
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
108 x(i) = i^2;
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
109 end
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
110
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
111 y = zeros(50,1);
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
112 k = 1;
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
113 step = 2;
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
114 while k<=(100-step)
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
115 y(i) = k^2;
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
116 k = k + step;
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
117 end
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
118 {% endhighlight %}
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
119
46
171ca967fcc9 Start new development branch closer to the current website,
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 43
diff changeset
120
171ca967fcc9 Start new development branch closer to the current website,
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 43
diff changeset
121 ### [Vectorization]({{site.docs_url}}Vectorization-and-Faster-Code-Execution.html)
23
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
122
43
a1cb50997055 Reviewed and reformatted some files.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 30
diff changeset
123 For-loops can often be replaced or simplified using vector syntax.
a1cb50997055 Reviewed and reformatted some files.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 30
diff changeset
124 The operators `*`,`/`,`^`,`%` all support element-wise operations using `.`.
a1cb50997055 Reviewed and reformatted some files.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 30
diff changeset
125 Many other functions operate element-wise by default (`sin`,`+`,`-`, etc.).
23
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
126
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
127 {% highlight matlab %}
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
128 i = 1:2:100; % create a 50-element array
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
129 x = i.^2; % each element is squared
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
130 y = x + 9; % add 9 to each element
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
131 z = y./i; % divide each element in y by the corresponding value in i
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
132 w = sin(i/10); % take the sine of each element ÷ 10
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
133 {% endhighlight %}
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
134
46
171ca967fcc9 Start new development branch closer to the current website,
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 43
diff changeset
135
171ca967fcc9 Start new development branch closer to the current website,
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 43
diff changeset
136 ### [Plotting]({{site.docs_url}}Two_002dDimensional-Plots.html)
23
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
137
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
138 `plot` can be called with vector arguments to create 2D line and scatter plots.
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
139
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
140 {% highlight matlab %}
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
141 plot(w,i/10);
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
142 title('w = sin(i/10)';
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
143 xlabel('i ÷ 10');
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
144 ylabel('w');
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
145 {% endhighlight %}
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
146
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
147 <img src="{{site.baseurl}}/img/sinePlot.png" alt="sine plot" style="height: 20rem; width: auto;" />
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
148
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
149
46
171ca967fcc9 Start new development branch closer to the current website,
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 43
diff changeset
150 ### [Strings]({{site.docs_url}}Strings.html)
23
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
151
43
a1cb50997055 Reviewed and reformatted some files.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 30
diff changeset
152 Strings are simply arrays of characters.
a1cb50997055 Reviewed and reformatted some files.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 30
diff changeset
153 Strings can be composed using `printf`-style formatting with `sprintf` and `fprintf`.
23
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
154 {% highlight matlab %}
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
155 firstString = 'hello world';
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
156 secondString = '!';
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
157 [firstString,secondString]
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
158 hello world!
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
159 fprintf('%s %.10f \n', 'The number is:', 10)
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
160 The number is: 10.0000000000
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
161 {% endhighlight %}
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
162
46
171ca967fcc9 Start new development branch closer to the current website,
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 43
diff changeset
163
171ca967fcc9 Start new development branch closer to the current website,
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 43
diff changeset
164 ### [If-else]({{site.docs_url}}The-if-Statement.html)
23
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
165
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
166 Conditional statements can be used to create branching logic in your code.
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
167
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
168 {% highlight matlab %}
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
169 % Print 'Foo' if divisible by 7,
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
170 % 'Fizz' if divisible by 3,
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
171 % 'Buzz' if divisible by 5,
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
172 % 'FizzBuzz' if divisible by 3 and 5
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
173 for (i = 1:1:100)
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
174 outputString = '';
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
175 if (rem(i,3) == 0) % rem is the remainder function
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
176 outputString = [outputString, 'Fizz'];
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
177 end
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
178 if (rem(i,5) == 0)
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
179 outputString = [outputString, 'Buzz'];
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
180 elseif (rem(i,7) == 0)
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
181 outputString = 'Foo';
43
a1cb50997055 Reviewed and reformatted some files.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 30
diff changeset
182 else
23
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
183 outputString = outputString;
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
184 end
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
185 fprintf('i=%g: %s \n',i,outputString);
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
186 end
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
187 {% endhighlight %}
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
188
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
189 {% highlight text %}
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
190 ...
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
191 i=12: Fizz
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
192 i=13:
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
193 i=14: Foo
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
194 i=15: FizzBuzz
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
195 ...
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
196 {% endhighlight %}
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
197
46
171ca967fcc9 Start new development branch closer to the current website,
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 43
diff changeset
198
171ca967fcc9 Start new development branch closer to the current website,
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 43
diff changeset
199 ### [Help]({{site.docs_url}}Simple-Examples.html#Help-and-Documentation)
23
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
200
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
201 The `help` and `doc` commands can be invoked at the Octave prompt to print documentation for any function.
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
202
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
203 {% highlight matlab %}
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
204 help plot
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
205 doc plot
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
206 {% endhighlight %}
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
207
46
171ca967fcc9 Start new development branch closer to the current website,
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 43
diff changeset
208
171ca967fcc9 Start new development branch closer to the current website,
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 43
diff changeset
209 ### [Packages]({{site.docs_url}}Packages.html)
23
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
210
43
a1cb50997055 Reviewed and reformatted some files.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 30
diff changeset
211 Community-developed packages can be added from the
a1cb50997055 Reviewed and reformatted some files.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 30
diff changeset
212 [Octave Forge](http://octave.sourceforge.net/index.html)
a1cb50997055 Reviewed and reformatted some files.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 30
diff changeset
213 to extend the functionality of Octave's core library.
a1cb50997055 Reviewed and reformatted some files.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 30
diff changeset
214 (Matlab users: Forge packages act similarly to Matlab's toolboxes.)
a1cb50997055 Reviewed and reformatted some files.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 30
diff changeset
215 The `pkg` command is used to manage these packages.
a1cb50997055 Reviewed and reformatted some files.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 30
diff changeset
216 For example, to use the image processing library from the Forge, use:
23
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
217
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
218 {% highlight matlab %}
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
219 pkg install -forge image % install package
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
220 pkg load image % load new functions into workspace
43
a1cb50997055 Reviewed and reformatted some files.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 30
diff changeset
221 {% endhighlight %}
23
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
222
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
223 <div id="assignmentModal" class="reveal-modal tiny" data-reveal aria-hidden="true" role="dialog">
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
224 {% highlight matlab %}
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
225 a = 1
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
226 b = a
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
227 b = 2
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
228 a == b % returns 0 (false)
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
229 {% endhighlight %}
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
230 <a class="close-reveal-modal" aria-label="Close">&#215;</a>
f37d56cd8195 add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff changeset
231 </div>