In [1]:
5+6
In [2]:
5*8
In [3]:
2^6
In [5]:
1 == 2 % False
In [6]:
1 ~= 2 % True
In [7]:
1 && 0 % AND Operator
In [8]:
1 || 0 % OR Operator
In [9]:
xor(1,0)
In [10]:
a = 3
In [11]:
a = 3; % semicolong supressing output
In [12]:
b = 'hi';
In [14]:
b
In [15]:
a= pi;
In [16]:
a
In [17]:
disp(a) % Display
In [18]:
disp(sprintf('2 decimals: %0.2f', a))
In [19]:
disp(sprintf('6 decimals: %0.6f', a))
In [20]:
a = 3.1416
In [21]:
format long
In [22]:
a
In [23]:
format short
In [24]:
a
In [25]:
A = [1 2; 3 4; 5 6]
In [26]:
A = [1 2;
3 4;
5 6]
In [27]:
v = [1 2 3]
In [28]:
v = [1;2;3]
In [30]:
v = 1:0.1:2
In [31]:
v = 1:6
In [33]:
# One matrix
ones(2,3)
In [34]:
C = 2*ones(2,3)
In [37]:
W = zeros(1,3)
In [35]:
w = rand(1,3)
In [36]:
rand(3,3)
In [44]:
w = -6 + sqrt(10)* (randn(1,10000));
hist(w, 100)
In [46]:
eye(4) % identity matrix 4x4
In [48]:
help eye % get help
In [50]:
A = [1 2;
3 4;
5 6]
In [51]:
# Returns the Size of the matrix 3x2
size(A)
In [52]:
size(A,2)
In [6]:
v = [1 2 3 4]
In [7]:
length(v)
In [8]:
# Who command shows the variables inside the octave workspace
who
In [9]:
# load('data.dat')
In [10]:
# Gives more detailed information about the variables
whos
In [11]:
# Clears the variable
clear A
In [12]:
whos
In [13]:
# without specifiying any name it clears all
clear
In [14]:
whos
In [15]:
A = [1 2; 3 4; 5 6]
In [16]:
# Indexing
A(3,2)
In [21]:
A(2,:) % ':' means every element along that row/column
In [20]:
A(:,2) % Returns ever
In [23]:
A(:,2) = [10 ; 11 ; 12] % Overwrite
In [25]:
A = [A, [100; 101; 102]]; % Append another column vector to right
A
In [26]:
size(A)
In [27]:
A(:) % put all elements of A into a single vector
In [30]:
A = [1 2; 3 4; 5 6];
B = [11 12; 13 14; 15 16];
In [33]:
C = [A B] % Concatenate A and B to make C
In [34]:
C = [A; B] % Adds second matrix add to bottom
In [35]:
size(C)
In [37]:
[A B] == [A, B]
In [3]:
% Defining our variables
A = [1 2; 3 4; 5 6];
B = [11 12; 13 14; 15 16];
C = [1 1; 2 2];
In [4]:
% Matrix Multiplication
A*C
In [5]:
% Element wise multiplication
A.*B
In [7]:
A.^2
In [8]:
v = [1;2;3];
1 ./ v
In [9]:
log(v)
In [10]:
exp(v)
In [11]:
% Absolute value, element wise
abs(v)
In [12]:
% Same as -1 * V
-v
In [13]:
v + ones(length(v), 1)
In [15]:
v
v + 1
In [16]:
% Taking the transpose
A'
In [17]:
a = [1 15 2 0.5];
val = max(a)
In [19]:
% Returning the index of the max value
[val, ind] = max(a)
In [20]:
% Element wise comparison
a < 3
In [22]:
% Returns the indexes where the statement true
find(a < 3)
In [23]:
A = magic(3)
In [24]:
[r, c] = find(A >= 7)
In [25]:
A(2, 3)
In [26]:
a
sum(a)
In [27]:
prod(a)
In [28]:
floor(a)
In [29]:
ceil(a)
In [30]:
max(rand(3), rand(3))
In [32]:
max(A, [], 1) % Take the first dimension of A
In [33]:
max(A, [], 2)
In [35]:
max(A) % Default
In [36]:
max(max(A))
In [37]:
max(A(:))
In [38]:
A = magic(9)
In [40]:
sum(A,1) % Sums the columns
In [41]:
sum(A,2) % Sums the rows
In [43]:
% Summing up the diagonal
sum(sum(A.* eye(9) ))
In [48]:
A.*eye(9)
In [45]:
% Summing up the other diagonal
sum(sum(A.*flipud(eye(9))))
In [47]:
A.*flipud(eye(9))
In [49]:
A = magic(3);
% Pseudo inverse
pinv(A)
In [50]:
temp = pinv(A);
temp*A
In [51]:
t = [0:0.01:0.98];
y1 = sin(2*pi*4*t);
plot(t, y1)
In [54]:
y2 = cos(2*pi*4*t);
plot(t, y2)
In [59]:
plot(t, y1);
hold on;
plot(t, y2, 'r'); % 'r' color indicator
xlabel('time');
ylabel('value');
legend('sin', 'cos');
title('my plot');
print -dpng 'myplot.png' % Saving the plot
In [60]:
close % Dissapear a figure
In [61]:
% Opening more than one figure by definin figure
figure(1); plot(t, y1);
figure(2); plot(t, y2);
In [66]:
subplot(1,2,1); % Divides plot a 1x2 grid, access first element
plot(t,y1);
subplot(1,2,2);
plot(t,y2);
axis([.5 1 -1 1]);
In [67]:
A = magic(5)
In [70]:
imagesc(magic(15)), colorbar, colormap gray;
In [71]:
v = zeros(10, 1)
In [73]:
% For Syntax:
for i=1:10,
v(i) = 2 ^ i;
end;
v
In [74]:
indices = 1:10; % Explicitly defining indices
for i=indices,
disp(i);
end;
In [75]:
i = 1;
while i <= 5,
v(i) = 100;
i += 1;
end;
v
In [77]:
i = 1;
while true,
v(i) = 999;
i += 1;
if i == 6,
break; % Usage of break
end;
end;
v
In [79]:
% General syntax for if
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;
Creating functions is basically creating a file with function name.m
In [85]:
% The function should be in the same directory or we can direct it with cd
squareThisNumber(64)
In [86]:
64^2
In [89]:
% We can define a function returns more than one value
[a,b] = squareAndCubeThisNumber(5);
a
b
In [90]:
% Design matrix
X = [1 1; 1 2; 1 3];
y = [1;2;3];
theta = [0;1];
In [91]:
costFunctionJ(X, y, theta)
In [92]:
theta = [0;0];
costFunctionJ(X,y,theta)
Unvectorized: $$ h_{\theta}(x) = \sum_{j=0}^{n} {\theta_{j}x_{j}} $$
or vectorized:
$$ h_{\theta}(x) = \theta^Tx $$These two will give different implementations
Indexing starts from 1 in matlab/octave
Unvectorized implementation:
prediction = 0.0;
for j = 1:n+1,
prediction += theta(j) * x(j);
end;
Vectorized implementation:
prediction = theta' * x;
In [93]:
A = [1 2; 3 4; 5 6];
B = [1 2 3; 4 5 6];
In [94]:
C = A*B