https://github.com/cheatsheet-lang/octave
Octave Cheatsheet
https://github.com/cheatsheet-lang/octave
conditional function hacktoberfest hacktoberfest2020 matrices octave octave-cheatsheet plotting statements syntax vectors
Last synced: 8 months ago
JSON representation
Octave Cheatsheet
- Host: GitHub
- URL: https://github.com/cheatsheet-lang/octave
- Owner: Cheatsheet-lang
- License: mit
- Created: 2019-12-26T11:06:48.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-01-10T14:03:18.000Z (over 5 years ago)
- Last Synced: 2025-01-03T09:16:57.226Z (9 months ago)
- Topics: conditional, function, hacktoberfest, hacktoberfest2020, matrices, octave, octave-cheatsheet, plotting, statements, syntax, vectors
- Homepage:
- Size: 10.7 KB
- Stars: 6
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Octave Cheatsheet
### General Commands
```octave
PS1("custom>> "); % For the Custom prompta = 3; % The semi-colon supresses the output
help(eye) % Gives some description about the eye function
who % Displays all the variables in the scope
whos % Gives the detailed display of variablesclear a % Deletes the variable a
clc % Clear Screen
close % Close the given figuresave variable.mat a; % Saves the variable a in the file
save variable.txt a -ascii; % Saves the variable a in human readablepause; % Go into an infinte sleep mode, by pressing enter you can return to prompt
```## Basic Operations
#### Variables
```octave
a = 3;
b = 'hi';
```#### Output
```octave
a = pi;
a % Simply give at the prompt
disp(a); % print function in octave
disp(sprintf('Vaue of pi is: %0.2f', a)) % C style printing
```#### Arthimetic Operations
```octave
5 + 6
5 - 4
5 * 8
1 / 2
2 ^ 6 % Exponentation
```#### Logical Opeartions
```octave
1 == 2 % false
1 ~= 2 % not equal to
1 && 0 % AND
1 || 0 % OR
xor(1, 0)
```## Vectors and Matrices
#### Vectors
```octave
row = [1, 2, 3] % Row vector , is a separator in a rowcol = [1; 2; 3] % Column vector ; separates the rows
length(row) % Gives the length of vector/ length of longest dimension in matrix
```## In-built Functions
#### Mathematical
```octave
v = [0.26, 2.22, 4]
matrix = [1, 23; 30, 4; 5, 6]log(3) % Logrithmic
exp([1, 2]) % Exponential
abs([-1, 2, -3]) % Absolute
-v % similar to -1 * v[val, ind] = max(v) % Returns an array of which has max value and it's index
max(matrix) % Returns column-wise maximumfind([1, 0, 3, 2]) % Returns the indexes of all the non-zero elements
magic(3) % returns a 3x3 magic square matrixsum(matrix) % Sum of a matrix
floor(v)
ceil(v)
```#### Matrices
```octave
matrix = [1, 2; 3, 4; 5, 6]
eye(3) % Diagonal matrix of order 3
ones(2, 3) % [1, 1, 1; 1, 1, 1]
zeros(1, 3) % [0, 0, 0]
mat = rand(3, 2) % A random 3x2 matrix
randn(1, 100) % Random matrix following a normal distributionsize(matrix) % Gives the dimensions of the matrix as a matrix[row, column] size(, [axis])
matrix(3, 2) % A usual matrix indexing
matrix(:,2) % : means all the elements in the corrsponding row/column.
matrix([1 3],:) % Everything from the 1st and 3rd rowmatrix = [matrix, [7; 8; 9]] % Appending a row to the matrix(right side)
matrix(:, 3) = [10; 11; 12] % Assiging a row
matrix(:) % Put all the elements into a single column vectormat_concat = [matrix mat] % Concatenate matrices side by side [matrix, mat]
mat_concat = [matrix; mat] % Concatenate matrices one above the othermatrix * mat % Matrix multiplication
matrix .* mat % This is element-wise multiplication, similarly .+, ./, .^matrix' % Matrix transpose
transpose(matrix)pinv(matrix) % Pseudo Inverse matrix
```#### Iterators
```octave
v = 1:0.1:2 % Start:step-size:end deafult step-size is 1
```## Plotting
```octave
a = magic(5)
plot(x, y) % Makes a two dimensional graph
hold on; % To plot one or more plots at a time
xlabel("label") % Label on the x-axis
ylabel("label") % Label on the y-axisprint -dpng '' % To save into a file
subplot(1, 2, 1) % Divide the figure into 1 row 2 columns and access the 1st figureimagesc(a), colorbar, colormap gray; % Plots using colors
hist(randn(1, 100)) % Plots a histogram hist(, [bins])
```## Control Statements
#### If else
```octave
v = zeros(10, 1);i = 0;
while true,
v(i) = i*i;
if i == 6,
break;
else
disp("Value is not 6")
end;
end;
```#### For
```octave
v = zeros(10, 1);for i=1:10,
v(i) = i*i;
end;
```#### While
```octave
v = zeros(10, 1);i = 0;
while i < 5,
v(i) = 10*i;
i = i + 1;
end;
```#### Functions
```octave
function y = squareNumber(x),
y = x ^ 2;
end;
```