Song Jiaming | 31 Oct 2017
    
Octave is a mathematical programming language. It is a good alternative to Matlab.
In this blog, I will list down some basic but useful commands of it.
| Description | Commands | Example/Remark | 
|---|---|---|
| Show >> | PS1(‘>>’) | - | 
| Help | help eye | Get description for eye function | 
| Show path | pwd | where you currently at | 
| Show files | ls | list all directories in this file | 
| Move to a file | cd ‘File Path | Go to the destinated directory | 
| Load data file | load xxx.dat == load(‘xxx.dat’) | - | 
| Show varables | who | varaibles I have in current scope | 
| Show varables 2 | whos | view more details | 
| User input | input(‘’); | name=input(‘SJM’); | 
| Clear | clear | - | 
| Save | save hello.mat v | Save v into hello.mat (binary format) | 
| Save 2 | save hello.txt v -ascii | human readable file | 
| Description | Commands | Example/Remark | 
|---|---|---|
| Power | ^ | x^2 | 
| Comment | % | - | 
| Equal | == | 1==2 | 
| Not equal | ~= | 1~=2 | 
| And | && | - | 
| Or | || | - | 
| XOR | xor(m,n) | xor(1,0)=1, xor(1,1)=0 | 
| Supress output | ; | a=3; No output will be shown | 
| String | ’’ | b = ‘hi’ | 
| Pi | pi | pi | 
| disp(a) | - | |
| Printf | disp(sprintf(‘2 decimals: %0.2f’,a)) | |
| Format | format long/short | How many digits to display | 
| Histogram | hist(w) | Histogram | 
| Histogram 2 | hist(w,n) | hist(randn(10000,1),30) (30 columns) | 
| Description | Commands | Example/Remark | 
|---|---|---|
| Row Vector | [ ] | [ 1 2 3 ] | 
| Column vector | [;] | [1;2;3] | 
| Matrix | [space and ;] | A = [1 2;3 4;5 6] | 
| Sequence | start:step:stop | 1:0.1:2 (2 is included) | 
| Matrix of 1 | ones(m,n) | ones(2,3) | 
| Matrix of 0 | zeros(m,n) | zeros(2,3) | 
| Random number | rand(m,n) | rand(2,3) | 
| Random number 2 | rand(m,n) | values follows Gaussian Distribution | 
| Identity matrix | eye(n) | eye(4) | 
| Magic matrix | magic(n) | nxn matrix with the same row/column/diag sum | 
| Lower Triangular | tril(A) | make A lower triangular | 
| Upper Triangular | triu(A) | make A upper triangular | 
| Description | Commands | Example/Remark | 
|---|---|---|
| Size | size(A) | A: Matrix | 
| Num of rows | size(A,1) | 1st dimension of A | 
| Num of columns | size(A,2) | 2nd dimmension of A | 
| Length | length(v) | length of vector v | 
| Get an element | A(m,n) | element at row m col n | 
| Get a row | A(m,:) | all element along mth row | 
| Get a column | A(:,n) | all element along nth column | 
| Get rows | A([1 3],:) | all element from 1st and 3rd row | 
| Append column | A = [A,[1;2;3]] | Append col at the end of A | 
| Flattern | A(:) | put A into a single verctor(column by column) | 
| Concatenate | [A B] | concatenate A and B | 
| Concatenate 2 | [A;B] | put B below A | 
| Get part of array | v(1:10) | first 10 element of v | 
| Description | Commands | Example/Remark | 
|---|---|---|
| Multiplication | * | A*B | 
| Multiply piecewisely | .* | A.*B | 
| Divide piecewisely | ./ | 1./v | 
| Log | log(v) | - | 
| Exponential | exp(v) | - | 
| Absolute | abs(v) | |
| Transpose | ’ | A’ | 
| Max of vector | max(v) | v is a vector | 
| Max and its index | [val, ind] = max(v) | val=max(v), ind = index of val | 
| Max of matrix | max(A) | max of each col of A | 
| Filter a vector with a critera | v < n | if (the entry of v) < n, return 1, else 0 | 
| Filter a vector 2 | find(v < n) | show index of entries in v which are less than n | 
| Filter a matrix with a critera | [r,c]=find(A>=n) | r=row index, c=col index, of the entry in A that is > n | 
| Sum | sum(v) | sum of all entries in v | 
| Sum by column | sum(A,1) | each column sum up of A | 
| Sum by row | sum(A,2) | each row sum up of A | 
| Product | prod(v) | product of all entries in v | 
| Floor | floor(v) | floor of all entries | 
| Ceiling | ceil(v) | ceiling of all entries | 
| Max between 2 matrices | max(A,B) | take max of entry of A,B | 
| Column-wise max | max(A,[],1) | column wise maximum of A | 
| Row-wise max | max(A,[],2) | row wise maximum of A | 
| Max of matrix | max(max(A)) | - | 
| Flipping | flipud(A) | 1230->0321 | 
| Pseudo inverse | pinv(A) | - | 
Let y1 = sin(t),y2 = cos(t)
plot(t,y1)hold onplot(t, y2, 'r')axis[x1,x2. y1,y2]xlabel('time')ylabel('value')legend('sin', 'cos')title('my plot')print -dpng 'myPlot.png'closeclfsubplot(m,n)subplot(m,n,i)imagesc(A)imagesc(A), colourbar, colormap gray;for i = 1:10,
    v(i) = 2^i;
end;
Or,
indices = 1:10
for i=indices,
    disp(i)
end;
i=1;
while i<=5,
    v(i) =100;
    i=i+1;
end;
Or,
while true,
    v(i) = 999;
    I = i+1;
    if i==6,
        break;
    end;
end;
if v(1) ==1,
   statement;
elseif v(1) ==2,
   disp();
else
  statement;
end;
Below, nameOfFunction is the function name, x is the parameter.
 
This function returns y.
function y = nameOfFunction(x)
y=x^2;
end
  nameOfFunction(2) => ans = 4Note: Let twoValues() be a function which returns 2 values [c,d]
Then [a,b] = twoValues() gives a=c and b=d.