Skip to content

Instantly share code, notes, and snippets.

@ssledz
Last active March 14, 2017 20:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssledz/e423b8b7d5ccf6354f4d65bb468583db to your computer and use it in GitHub Desktop.
Save ssledz/e423b8b7d5ccf6354f4d65bb468583db to your computer and use it in GitHub Desktop.

installation

sudo add-apt-repository ppa:octave/stable
sudo apt-get update
sudo apt-get install octave

resources

executable script

#! /bin/octave -qf
printf ("%s", program_name ());
arg_list = argv ();
for i = 1:nargin
  printf (" %s", arg_list{i});
endfor
printf ("\n");

coments

# single line comment
 #{
  multi line
  comment
 #}

data types

structure

x.a = 1;
x.b = [1, 2; 3, 4];
x.c = 'string';

basic opertions

5+6
3-2
5*8
1/2
2^6
1 == 2 % false
1 ~= 2 % not equals
1 && 0 % AND
1 || 0 % OR
xor(1,0)
PS1('>> ') % change prompt 1
a = 3
a = 3; % semicolon supressing output
b = 'hi'
c = (3 >= 1)
a = pi;
disp(a)
disp(sprintf('2 decimals: %0.2f', a))
disp(sprintf('6 decimals: %0.6f', a))
format long
format short
A = [1 2; 3 4; 5 6]
v = [1; 2; 3] % a vector 3x1
v = 1:0.1:2
v = 1:6
ones(2,3)
c = 2 * ones(2,3)
w = ones(1,3)
w = zeros(1,3)
w = rand(1,3)
rand(3,3)
w = randn(1,3)
w = randn(1,3) % gaussian distribution mean=0 stdev=1
rand(1,3) % uniform distribution
w = -6 + sqrt(10) * (randn(1,10));
w = -6 + sqrt(10) * (randn(1,10000));
hist(w)
hist(w, 50)
eye(4) % 4x4 identity matrix
help eye
help rand
help help
help history
history -q

moving data around

A = [1 2; 3 4; 5 6]
size(A) % [3 2]
size(A, 1) % give first dimension (3)
size(A, 2) % give second dimension (2)
v = [1 2 3 4]
length(v)
length(A) % size of the longest dimension (for A is 3)
pwd % current dir
cd /home/ssledz/work
ls
load featuresX.dat 
load priceY.dat
load('featuresX.dat')
who
featuresX
size(featuresX)
priceY
size(priceY)
whos % more detailed who
clear featuresX
v = priceY(1:10)
save hello.mat v;
clear % removes all variables from the workspace
load hello.mat 
save hello.txt v -ascii % save as text (ASCII)
A = [1 2; 3 4; 5 6]
A(3,2)
A(2,:) % fetch everything in a second row
A(:,2) % fetch everything in a second column
A([1 3],:) % fetch entire 1 and 3 row 
A(:, 2) = [10; 11; 12] % assign the second column to vector [10; 11; 12]
A = [A, [100; 101; 102]] % append another column vector to the right
A(:) % put all element of A into single vector
A(:) % put all element of A into single vector (9x1 vector)
A = [1 2; 3 4; 5 6]
B = [11 12; 13 14; 15 16]
C = [A B]
C = [A;B]
[A B] == [A,B]

computing on data

A = [1 2; 3 4; 5 6]
B = [11 12; 13 14; 15 16]
C = [1 1; 2 2]
A*C
A .* B
A .^ 2
v = [1; 2; 3]
1 ./v
1 ./ A
log(v)
exp(v)
abs(v)
abs([-1; 2; -3])
-v % -1 * v
v + ones(length(v), 1)
v + ones(length(v), 1) % same as v + 1
A'
(A')'
a = [1 15 2 0.5]
val = max(a)
[val ind] = max(a)
a < 3
find(a < 3) % returns matrix with indexes reflecting given condition
 A = magic(3)
[r c] = find(A >= 7)
sum(a)
prod(a) % product
floor(a) % rounds down
ceil(a) % rounds up
max(rand(3), rand(3)) % element wise max
max(A,[],1) % column wise max
max(A,[],2) % per row  max
max(A) % column wise max
max(max(A)) % max value from the matrix A
max(A(:)) % max value from the matrix A
A = magic(9)
sum(A,1) % per column sum
sum(A,2) % per row sum
sum(sum(A.*eye(9))) % sum of diagonal elements
flipud(eye(9)) % oposite diagonal matrix with ones
sum(sum(A.*flipud(eye(9)))) % sum of second diagonal elements
A = magic(3)
pinv(A) % sudo invert matrix

ploting data

t=[0:0.01:0.98];
y1=sin(2*pi*4*t);
plot(t, y1);
y2=cos(2*pi*4*t);
plot(t, y2);
plot(t, y1);
hold on;
plot(t, y2, 'r');
xlabel('time');
ylabel('value');
legend('sin', 'cos');
title('my plot');
graphics_toolkit ("gnuplot")
setenv ("GNUTERM", "x11")
print -dpng 'myPlot.png'
figure(1); plot(t, y1);
figure(2); plot(t, y2);
subplot(1,2,1); % divides plot a 1x2 grid, access first element
plot(t, y1);
subplot(1,2,2); % divides plot a 1x2 grid, access second element
plot(t, y2);
axis([0.5 1 -1 1]) % sets range for x and y
clf; % clear figure
A = magic(5)
imagesc(A)
imagesc(A), colorbar, colormap gray;

control statements: for, while, if

v=zeros(10,1)
for i=1:10,
  v(i) = 2 ^ i;
end;
indices=1:10;
for i=indices,
  v(i) = 2 ^ i;
end;
i = 1;
while i <= 5,
  v(i) = 100;
  i = i + 1;
end
i = 1;
while true,
  v(i) = 999;
  i = i + 1;
  if i == 6,
    break;
  end;
end;
v
v(1) = 2;
if v(1) == 1,
  disp('The value is one');
elseif v(1) == 2,
  disp('The value is two');
else
  disp('The value is not one or two.')
end;

ls
squareThisNumber(2)
% Octave search path
addpath('/home/ssledz/work') % add this direcotry to the octave search path
squareThisNumber(2)
squareAndCubeThisNumber(5)
[y1,y2] = squareAndCubeThisNumber(5)
X = [1 1; 1 2; 1 3];
y = [1; 2; 3]
theta=[0;1]
j = costFunctionJ(X, y, theta)

squareThisNumber.m

function y = squareThisNumber(x)
y = x^2;

squareAndCubeThisNumber.m

function [y1,y2] = squareAndCubeThisNumber(x)
y1 = x^2;
y2 = x^3;

costFunctionJ.m

function J = costFunctionJ(X, y, theta)
m = size(X,1);
prediction = X*theta;
sqrErrors = (prediction-y).^2;
J= 1 / (2 * m) * sum(sqrErrors);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment