Octave Crash Einführung
Octave Crash Introduction
% This is a comment because of the "%"
%-----------------------------------------------------------
% Print something to the text output:
%-----------------------------------------------------------
% Note: Octave accepts " and ' for strings, Matlab only '
disp('This is a text, an end line will be automatically added');
% Note: Octave knows printf like C/C++, Matlab only sprintf, which returns a string, so:
disp(sprintf('The answer to all questions is %d', 42));
%-----------------------------------------------------------
% Variables and types
%-----------------------------------------------------------
% Set some variables
% Note: if no semicolon is added at the end of line,
% the content of the variable will directly printed as
% text output:
text = 'This is a text' % A string
number = 3.1415 % All normal numbers are doubles / 1x1 matrices
row_vector1 = [1,2,3,4,5] % The columns are split with , or blank, [] means matrix
row_vector2 = [1 2 3 4 5] % Here space separated, also a (1,5) matrix
column_vector = [1;2;3;4;5] % This is a row vector, or also interpreted as (5,1) matrix
A = [1,2,3; 4,5,6; 7,8,9] % a matrix with three columns and three rows
B = [1,0,0; 0,1,0; 0,0,1] % Norm matrix, also known as function eye(3).
C = eye(3) % ... as here
% Note: Matlab and Octave are case sensitive, hence variable A is not variable a.
a = 'I am not A'
% Cell arrays, which is almost the same as arrays/collections in other programming languages
cells{1} = 'A string field';
cells{2} = 42;
cells{3} = pi()
% Structures (like in C) are like a folder structure in the file system,
% but separated with a dot:
myVector.x = 10;
myVector.y = 11;
myVector.z = 12
% Note: There are more data types like fixed point integers etc, refer to the octave manual.
%-----------------------------------------------------------
% Clauses
%-----------------------------------------------------------
% if clause, elseif and else:
% Use && for locical AND
% Use || for logical OR
% Use == for equals
% Use ~= for NOT equal
a = 100;
b = 1;
if a == 1001
disp('a is 101'); % This will not be displayed, as 100 is not 1001
elseif a == 100 && b == 10
disp('a is 100 and b is 10'); % This will not be displayed, as 1 is not 10
elseif a == 100 || b == 10
disp('a is 100 or b is 10'); % This will be displayed, as a is indeed 100
else
disp('a and b are something else');
end
%-----------------------------------------------------------
% Loops
%-----------------------------------------------------------
% Count from 0 to 5 (for loop).
% Note: Principally 0:5 generates the vector [1,2,3,4,5], and
% the for loop loops through each of the elements of the vector.
% Note: Because i and j are preset with the imaginary unit of complex numbers
% people often use k as loop variable, not i like in other programming languages.
% However, you can use i and j as well, it does normally not hurt ...
for k=0:5
disp(sprintf('Now k=%d', k));
end
% Count from 0 to 10 in steps of 2
for k=0:2:10
disp(sprintf('Now k=%d', k));
end
%-----------------------------------------------------------
% Plotting
%-----------------------------------------------------------
% Generate a vector [1,2,3,4, ..., 99, 100];
x = 1:100;
% Calculate the square root for each of the elements of the vector
% This is only that we have some "interesting" values to plot ...
y = sqrt(x);
% Now x and y are vectors with the length 100, this is important for plotting.
% Ok, new figure, please:
f = figure();
% Plot y over x:
plot(x,y);
% We want a grid displayed:
grid on;
% Title and labels:
title('y over x');
xlabel('x');
ylabel('y');
%-----------------------------------------------------------
% Load and save files
%-----------------------------------------------------------
% Example only: load CVS data, there are loads of format for the function load()
%cvs_data = load('-ascii', '/path/to/my/file')
x = 1:100; % x = vector from 1 to 100
y = x.^2; % square of each element of x
% Example only: save has loads of options. Here we save x and y in file.txt ...
%save ('-ascii', 'file.txt', 'x','y');
%-----------------------------------------------------------
% Some traps
%-----------------------------------------------------------
% Matlab/Octave always work with matrices if possible, means:
dot_product = [1,2,3] * [4;5;6] % this is = 32
% returns 32, which is 1*4+2*5+3*6
% note: the matrix product must be (1,3) * (3,1), not (1,3) * (1,3),
% otherwise the interpreter will complain because of the matrix dimensions.
% to make a row vector to a colum vector, use the "'" like:
A = [1,2,3]' % this is [1;2;3], now with ; instead of ,
% For matrices this is the tansposed matrix:
A = [1,2,3; 4,5,6; 7,8,9]' % this is = [1,4,7; 2,5,8, 3,6,9]
% To get the product of each elemens of the vectors or matrix, use .*
element_product = [1,2,3] .* [4,5,6] % this is = [4, 10, 18]
% ths same is for division (./) and power of (.^). For plus and minus
% there is no need to set a dot because the matrix operation is already
% to add or substract each element:
plus = [1,2,3] + [3,2,1] % this is = [4,4,4]
minus = [1,2,3] - [3,2,1] % this is = [-2,0,2]