In [1]:
5+6
3-2
2^6
1 == 2 % False
1 ~= 2 % ~= not equals
In [2]:
1 && 0 % and
1 || 0 % or
xor(1,0) % xor
In [3]:
a = 3; % semicolon suppresses output
In [4]:
b = 'hi'
c = (3 > 1)
In [5]:
a = pi
In [6]:
disp(a); % octaves version of print
In [7]:
disp(sprintf('2 decimals: %0.2f', a)) % c style printf
In [8]:
format long % defaults strings to long format
a
In [9]:
format short % revert
a
In [10]:
A = [1 2; 3 4; 5 6]
In [11]:
v = [1, 2, 3] % 1x3 row vector
In [12]:
v = [1; 2; 3] % 3x1 column vector
In [13]:
v = 1:0.1:2 % v from 1 to 2 step 0.1
% returns a row vector
In [14]:
v = 1:6
In [15]:
ones(2,3)
In [16]:
C = 2 * ones(2, 3)
In [17]:
w = ones(1, 3)
In [18]:
w = zeros(1, 3)
In [19]:
w = rand(1, 3) % between 0 and 1
In [20]:
w = randn(1, 3) % from gausian dist
In [21]:
w = -6 + sqrt(10) * randn(1, 10000);
In [22]:
hist(w)
In [23]:
hist(w, 50) % 50 bins
In [24]:
eye(4) % 4x4 identity matrix
In [25]:
help eye
In [26]:
A
In [27]:
size(A) % returns a 1x2 matrix
In [28]:
size(A, 1) % num of rows
size(A, 2) % num of cols
In [29]:
v = [1, 2, 3, 4];
length(v) % returns longest dimension
In [30]:
pwd % current directory
In [31]:
cd '/home/gary/Devel/python/coursera_ml' % change directory
In [32]:
ls
In [33]:
load featuresX.dat
load priceY.dat
% load('featuresX.dat) equivalent
In [34]:
who % variable in workspace
In [35]:
size(featuresX)
In [36]:
whos % detail view of who
In [37]:
% clears featuresX % remove from workspace
In [38]:
v = priceY(1:10)
In [39]:
save hello.mat v; % add --ascii for plain text
In [40]:
A
In [41]:
A(3,2)
In [42]:
A(2,:) % Everything in the second row
In [43]:
A(:,2) % Everything in the second column
In [44]:
A([1,3],:) % rows 1 and 3
In [45]:
A(:,2) = [10; 11; 12] % Replace a column
In [46]:
A = [A, [100; 101; 102]] % Append another column vector to the right
In [48]:
A(:) % put all elements of A into a single vector
In [50]:
A = [1, 2; 3, 4; 5, 6]
B = [11, 12; 13, 14; 15, 16]
In [55]:
C = [A B] % Concatenation
% [A B] = [A, B]
In [56]:
C = [A; B] % Concat at the bottom
In [ ]: