0% found this document useful (0 votes)
652 views7 pages

An Octave Introduction Cheat Sheet

As the name suggests, it's handy for usersof OCTAVE.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
652 views7 pages

An Octave Introduction Cheat Sheet

As the name suggests, it's handy for usersof OCTAVE.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 7
Octave CheatSheet GNU Octave is a high-level interpreted language, primarily intended for numerical computations (via GNU Octave) > Basics * not equal ~= * logical AND aa # logical OR || * logical XOR xor(1,) Asemicolon ; at the end of a e will suppress the output v = 1:5 creates a 1x5 Matrix (linear vector) with the numbers one to five. v = vector but with the numbers from one to two with a step size of 0.1. :.1:2 also creates a 1.0000 1.1002 1.2000 © 1.3000 += 1.4000 «1,500 1.6000 1.7000 1.8000 1.90¢ To display the Sth number of this vectors use v(5). Beware that ocatve is 1 based. Displaying a range of numbers use v(3:5)to display the 3rd, 4th, and Sth number. ? Useful functions help _function name_will give you the man page of this function ones(x [, y]) creates a matrix with ones. If only x is provided it is a squared matrix of size x. If y is provides it has the size x cross y. zeros(x [, y]) behaves the same as ones() but will give a zero-matrix. rand(x [, y]) Return a matrix with random elements uniformly distributed on the interval (0, 1). randn(x [, y]) Return a matrix with normally distributed random elements having zero mean and variance one. The arguments are handled the same as the arguments for rand. hist(x [, y]) Produce histogram counts of plots. y is the number of buckets. eye(x [, y]]) Produces an identity matrix. size(A [, DIM]) Return the number of rows and colums of A. bIm = 1 number rows DIM = 2 number columns length(A) Return the length of the object A. For Matrix objects, the length is the number of rows or columns, whichever is greater (this odd definition is used for compatibility with MATHLAB) log(a) % natural logarithn abs(a) % absolute value sign(a) % signum function exp(a) % compute e*a who Lists currently defined varibales matchin the given pattern. whos Provide detailed information on currently defined variables. sun(a), sum(A,1) sums up all columns. sun(A,2) sums up all rows prod(a) takes the product of each column floor(a) rounds down ceil(a) rounds up clear will clear all variables or only the ones who are named. ? Save and Load Data You can save and load data in octave easily with the two commands save and load. To save a specific variable v to the file filename.dat save filename.dat v; % save data of v in binary format save filename.txt v --ascii % save data of v in readable version Loading data is as simple as saving, % both are equivalent load filename.dat load(' filename.dat') The file will be saved in the current directory and will be loaded from the current dir. ? Matrixes Let abe the matrix: A = [1 2; 3 4; 5 6] The semicolon indicates a new row and a space or comma is a new column. Instead of typing a semicolon it is also possible to hit enter. Output last element in Matrix >> A(end, end) ans = 6 > Display Data % show number in first row and second column >> A(1,2) ans = 2 % show second row: colon means ‘all’ >> A(2,2) ans = 3 4 % show second column >> A(:,2) > Assign new Data % replace second colunn by 10, 11, and 12 >> A(,2) = [10,11,12] % in this case it doesn't matter if comma or semicolon is used >> A(,2) = (10513312) > Concatinating Matrixes >> B= [20 21; 22 23; 24 25] >> C= [AB] AB ans = 24. 25 >> D = [As B] % A append B 1 2 20 2a 4 22 23 6 24 25 > Transpose a matrix or a vector >> At ans = > max values and find) >> max(magic(4)) % returns a 1x4 vector with max-values per colunn ans = 0 4S >> [val, ind] = max(magic(4)) % retuns 2 vectors: 1. values, 2. indexes val = 16 14 15 2B ind = 1 4 4 1 To find the largest value in a matrix you can either chain to max() functions or take the complete matrix, >> max(max(A)); % Both are equivalent >> max(A(:)) You can search thru matrixes and vectors with a condition >> find(A < 3) % returns a index-vector where number < 3 > Flipping a matrix Sometimes it is necessary to flip a matrix upsidedown. >> flipud(a) 5 6 3 4 1 2 Hint: useful in combination with the identity matrix flipud(eye(4)) > Sum of diagonals in a square matrix Example: A magic matrix is a square matrix where the sum of rows, columns and diagonals are the same result. Let's check the sum of diagonals >> M = magic(4); >> sum(sum(M.*eye(4))) % sum of diagonal top left to bottom right ans = 34 >> sum(sum(M.* flipud(eye(4) ))) % sum of diagonal bottom left to top right ans = 34. > Inverse matrix There exists serveral ways to create a inverse matrix in octave >> inv(M) % warning: yes >> pinv(m) % warning: no ares % warning: no >> M\eye(size(M)) ——-X warning: yes >Ploting plot(x, y) plot a graph with x as x-axis and y as y-axis. x and y has to match in length hold on will plot updated in the current plot instead of replace the current plot. plot(x, y2, ‘r') will plot a graph in red. axis([a b ¢ dj) X-axis will start at a and goes to b, and Y-axis will start from c and goes to d. Example axis({@.5 1-1 1) elf; clear figure imagesc(A) grid of colors for matrix colorbar only gray-scale isplay colorbar, kind of legend colornap gray display Example: imagesc(A), colorbar, colornap grays > Labels xlabel('Zoidberg'), ylabel('Bender'), legend(‘fry', ‘lila'), title('Futurama’) print -dpng ‘nyPlot.png" export plot as PNG file. For other format see help plot close close the plot, as simple as that. Figure(1); plot(x, y) plot graph as figure 1 Figure(2); plot(x, y2) plot graph as figure 2 subplot (x,y, POS); plot multiple graphs in one windows as a X-by-Y grid at position pos >> subplot (1,2,1)5 >> plot(x, y)5 >> subplot (1,2,2)5 >> plot(x, y2)5 > Functions & control statements Functions are saved in files with the file-ending .m for MATHLAS. The syntax is quite simple function y = function_name(x) y = x25 % y is the return value % x is a paraneter % is also possible to return multiple values function [y1, y2] = function_name(x) yl = x2 y2 = x3 Call your function in octave like function_nane(s). You have to be in the same dir as the file or add it to your search-path (addpath()). for- and while-loops are easy and useful in octave >> for isa: >» disp(4) >> ends > ieay >> while (i ~= 10) >» disp(4)s >» is isd; >> endwhile; Itis also possible to use if-conditions Ki=10 >> if (i == 18) >» sprintf(‘yes') >> else >» sprintf(‘no") >> endif ans = yes Various The following commands can be used within octave! ocd © pwd © is [-la] © dir It is possible to change octave's prompt. Use Ps1(‘>> '); to change the promt to >> isp(a); will display the variable a, Useful in loops or conditions. magic(x) Create an N-by-N magic square For more controll in displaying stuff use sprint#(‘xi", a);. It follows the C-standard, format long or format short adjusts the length of output in octave. addpath('_path_') will add a path to the search-path for octave. Le. Octave will also look in this path for functions and files.

You might also like