Octave Basics

First, let’s use Octave to run basic arithmetic operations:

Then, we can let Octave execute some logic or boolean operations. If the result is TRUE, Octave returns 1, and 0 for FALSE.  == means equals to, ~= means NOT equals to, && means AND, || means OR, and XOR operations can be used with the function xor() as well.

Now, we can learn to define variables in Octave. The definition operator is =.  The left side is the variable’s name and the right side is the value. If you add a semicolon after the definition sentence, then Octave will NOT show you the variable onto the screen after the ENTER. If you add NO semicolon after the sentence, Octave will display the variable you defined immediately. You can type in the variable name or use the function disp() to show the variable’s value. In Octave, the famous irrational number π can be represented by a special built-in variable pi.

Let’s take a look at matrix and vector. Octave uses a pair of brackets to define a matrix or vector and the semicolon to separate different rows in a matrix. In rows, blank space is used for separating columns. You can choose to add an ENTER after each row or not. This won’t make any difference essentially except for a more beautiful look. You can also use the function ones(number_of_rows, number_of_columns) to define or construct a matrix and the default value is 1 for each element. The function zeros(number_of_rows, number_of_columns) can be used to construct matrix with 0-value elements.

The function rand(number_of_rows, number_of_columns) can construct matrix with random-value elements. Another function eye(number_of_rows, number_of_columns) can build an identity matrix. the function size(matrix_name, [row_number]) will return the size of a matrix or vector and row_number parameter is optional. If row_number is specified, this function will return the size of the corresponding row. Interestingly, the result returned is in the format of a matrix if the row_number is not specified.

Another useful function is hist(matrix_name, [bin_number]). This function will produce a histogram of the matrix. And the number of bins can also be specified in the second parameter. In our examples below, the function squrt(number) returns the square root of number and randn(number_of_rows, number_of_columns) returns a matrix with random-value elements, which is in a normal distribution.

To list variables, we can use the function who, which will display all variables in this workspace. Another function whos will display all variables in all workspaces. The function clear [variable_name] will clear the corresponding variable and clear all variables in this workspace if no variable’s name is explicitly specified.

To save our variables, we can use the function save. save output_file_name variable_name will save the corresponding variable into the corresponding file. And we can use the function load file_name to load the variable again.

That’s all for today. Feel free to contact me to talk about technical questions and experience.

Leave Comment

电子邮件地址不会被公开。 必填项已用*标注