# Octave Programming Language

Very high level langauge for numerical computations such as Matrix Math. Command Line Interface (with a simple GUI IDE available). ^  Change prompt with PS1('>>')

Seperate commands with , or ; Suppress responses by ending the line with ; if the line isn't terminated, an "ans = " will display the result. Use disp( ) to print to stdio without "ans =". sprintf available.

format short or format long for displayed resolution of numbers.

Any standard math command can be entered, E.g. 1+2 log() abs() exp() max() sum() floor() ceil(). -v returns the negative of v.

Equal is ==, Not equal is ~=. And &&, Or ||, XOR is xor( ).

False is 0, True is 1.

Variable assignment with =. Multiple values can be "broken out" on the left side with []. E.g. [val, ind] = max(a)

Strings in single quotes. hi = 'hello';

matrix [ row {; row}] where row is col { col}. E.g. A = [1 2; 3 4; 5 6] is a 2 by 3 matrix.

matrix(row, col) indexes a row and colum from the matrix variable. E.g. A(2, 1) returns the first value in the second row of A. Assuming A is defined as above, that would be 3.
matrix(row, :) indexes a row from the matrix variable, returning a vector. E.g. A(2, :) returns the second row of A. Assuming A is defined as above, that would be [3 4]. Note that A(2) does NOT return the entire row; it only returns the first column of the second row.
matrix(:, col) indexes a colum from the matrix variable, returning a 1xcol matrix. E.g. A(:, 1) returns the first column of A. Assuming A is defined as above, that would be [1; 3; 5]
matrix(matrix2, :) addresses the rows of the matrix indexed by matrix2. E.g. A([1 3]),:) returns the 1st and 3rd rows of matrix A. Assuming A is defined as above, that would be [1 2; 5 6]
matrix(:) returns a vector with all the rows and columns of A. E.g. A(:) returns [1;3;5;10;11;12]
NOTE: all the above can be used in assignments (on the left side) as well as returning values on the right side. e.g. A(:,2) = [10;11;12] replaces the second column of A with 10, 11, and 12
A row or column can be appended by makeing a new matrix using the old value of the matrix with the new values included and then assigning that to the matrix. e.g. A = [A, [100; 101; 102]] appends a new column making A a 3x3 matrix. C = [A B] joins two matrixes, etc... C = [A; B] joins them top to bottom.

All the standard matrix math functions are available. A*B; A/B; A+B; A-B; etc... For Matrix multiply or divide^, the second dimension of A must match the first dimension of B, and the result will be a matrix which is the first dimension of A by the second dimension of B.
If A is n x m and B is m x p the result AB will be n x p. [n x m]*[m x p] = [n x p]

Dot operations, such as .* , or .^ will apply each element by each element. e.g.

• A .^ 2 returns each element of A squared. Note: A ^ 2 is NOT the same; instead it computes A * A
• 1 ./ A returns the reciprocal of each element.
• v < 3 returns true or false for each element. True (1) if the element is less than 3, false (0) otherwise. See find() below.

Note: Uniary math operators are dot wise. e.g. log(A) takes the log of each element of A.

transpose(matrix) or just matrix.' (the matrix followed by a dot and single quote) transposes the matrix; makes the rows into columns and columns into rows.

ctranspose(matrix) or just matrix' (the matrix followed by a single quote) conjugate transpose of the matrix. Complex conjugate is a complex number with the sign of it's imaginary part swapped. (i.e x+iy ? x-iy or vice versa). The complex conjugate of a matrix is obtained by replacing each element by its complex conjugate. The conjugate transpose by doing that and transposing it.

flipud(matrix) Flip array upside down.
fliplr(matrix) Flip array left right.

ones(rows, cols) makes a rowsxcol matrix filled with 1's

eye(size) returns an identity matrix. E.g. eye(3) returns [1 0 0; 0 1 0; 0 0 1]
eye(rows, cols) makes an identity matrix of that size. e.g. A .* eye(3,2) returns [1 0; 0 4; 0 0]

rand(rows, cols) makes a rowsxcol matrix filled with random values between 0 and 1. multiply by a scaler to make a matrix with values between 1 and n; add or subtract to offset, etc...

randn(rows, cols) makes a rowsxcol matrix filled with gausian random values.

size(matrix) returns a 1x2 matrix showing the size of each dimision of the matrix.
size(matrix, dim) returns a scalar showing the size of the dim dimision of the matrix.

length(matrix) returns a scaler with the longest dimision of matrix. e.g. v + ones(length(v),1) returns a matrix with each element of v incremented. (but it turns out that this is the same as v + 1)

max(vector) returns a 1x2 matrix with the maximum value and the index at which that value was found. e.g. val = max(v) returns the maximum value found in vector v. [val, ind] = max(a) returns the maximum in val and the index where that value was found in ind.
max(matrix) returns the column-wise maximum value; the max of each row. To find the max value in the entire matrix, use max(max(matrix). or max(A(:))
max(matrix, [], dim) returns the maximum along the dim  diminsion.
max(matrix, matrix) returns the pair-wise maximum between the two matrix. e.g. max([1 2; 2 3; 5 6],[6 5; 4 3; 2 1]) returns [6 5; 4 3; 5 6]

find(vector) returns the position of the element if the element is true. E.g. find([1 15 2 0.5] < 3) returns [1 3 4] because the 1st, 3rd, and 4th elements are less than 3.
find(matrix) returns a matrix of positions of the elements which are true.

prod(vector) returns all the elements multiplied together.
prod(matrix) returns each columns elements multiplied together.

sum(matrix{, dim}) returns all the elements summed together along dimension dim. dim is assumed to be the first dimision that is not 1 if ommitted. e.g. sum(A) returns [9 12] and sum(A,2) returns [3;7;11]
Note: a common error is thinking that sum will return a scalar when used on a multidimisional matrix. To produce a scalar use sum(sum()) or convert the matrix to a vector using (:) e.g. sum(A(:))

inv(matrix) a-1 is inv(a). returns the inverse of the matrix which must be square and the determinant can not be zero. Inverse is the matrix version of the recriprocal of a number. a * a-1 will be the identity matrix of the same size. Division by a matrix is multiplication by the inverse of the matrix.

pinv(matrix) returns the pseudoinverse.

vector e.g. v = [1; 2; 3]

range start:inc:end where start is the starting value, end is the last value, and inc is the (optional) amount to increment. E.g. B = 1:0.1:2 makes a 1x11 matrix with values [1 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2], B = 1:3 makes a 1x3 matrix with values [1 2 3]

### File System

pwd Print Working Directory

cd path Change Directory

ls List Stuff(?) like DIR in DOS.

save file variable {-ascii} saves a memory variable to a file on the disk. The typical extension is .mat or .dat. The -ascii option makes the file human readable instead of the default binary.

who shows current memory variables
whos shows details about current memory variables; attr, size, bytes, class

clear variable erases a variable from memory. There is no undo.

### Plotting

hist(matrix) plots a histogram

plot(x, y) plot data points from y per x

hold on prevents a graph from being erased by the next plot command so that two vplots can be shown on one graph.
hold off

xlabel('string')
xlabel('string')
title('string')
legend(strings) add labels to the graph. use one string for each diminsion in the legend command.

axis ([X_lo X_hi {Y_lo Y_hi {Z_lo Z_hi}}]) Set axis limits

print -dpng file save the current graph to a file as a png.

close closes a graph.

figure(number) switches between multiple graphs.

subplot(rows, cols, number) divides up a graph into parts, and selects the part indicated by number.

imagesc(matrix) Display a scaled version of the matrixas a color image. Each element represents a color or intensity.
colorbar Add a colorbar (legend) to the current axes.
colormap gray use grey scale instead of colors.

### Control Statements

for variable = range, statements, end

while condition, statements, {break}, end

if condition, statements, {elseif condition}, {else}, end

function OUTPUTS = function (INPUT, ...) Multiple values can be "broken out" on the left side with [].

Anonymous Functions are defined using the syntax @(argument-list) expression ^ Example uses:

• Pass the output of one function as a parameter into another function without needing a temporary variable inbetween:
• Adapt a function with several parameters so that some of those parrameters are filled from the environment. In this example, the values of a and b that are passed to betainc are inherited from the current environment.
```a = 1;
b = 2;
quad (@(x) betainc (x, a, b), 0, 0.4)
```

Literals

• pi

 file: /Techref/language/octave.htm, 12KB, , updated: 2019/1/29 23:14, local time: 2024/9/8 08:20, TOP NEW HELP FIND:  44.220.247.152:LOG IN

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?Please DO link to this page! Digg it! / MAKE! Octave Programming Language

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.

Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
 Did you find what you needed? "No. I'm looking for: " "No. Take me to the search page." "No. Take me to the top so I can drill down by catagory" "No. I'm willing to pay for help, please refer me to a qualified consultant"

.