%% general commands

ver % software and toolboxes information

pwd % current Matlab path

dir % list files in current path

clear % clear all variables

clc % clear command window

%% row vectors

a = [1, 2, 3, 4, 5] % row vector
a = [1 2 3 4 5]
a = 1:5

b = 5:2:13; % suppress output
b
b = linspace(5, 13, 5)


b1 = 18:-3:9

c = [a b] % concatenate two vectors 

a2 = [a, a] % vector
repmat(a, 1, 2)

a10 = repmat(a, 1, 10)

[a; a] % matrix %
repmat(a, 2, 1)

zerovec = zeros(1,4)

vec1 = ones(1,8)

r = rand(1, 7)

fprintf('first element is %1.3f \n', r(1)) 

fprintf('last element is %1.3f \n', r(end)) 
% r(end) is same as r(7) in this case

whos
%% column vectors 

x = [2; 3; 4; 5; 6]

y = b'

z = [x; y]

z3 = [z; z; z]
z3 = repmat(z, 3, 1)

[z z z] % matrix
repmat(z,1,3)

%% vector attributes

length(a) % length of vector
numel(a) % number of elements 

size(a) % size of a (1x3)

length(x) 
numel(x)

size(x)

%% single vector operations

c
c(1:end-1)
c(2:end)

c(end:-1:1)
flip(c)
help flip

c(6:end)= -1 % set 6th to last entries = 0

asq = a.^2  % elementwise square
a.*a

x.^2

sum(a) % sum of entries in a

sum(x)

norm(a)  % Euclidean norm
sqrt(sum(asq))

cumsum(a) % vector of cumulative sums, default = 'forward'

cumsum(a, 'reverse') % backward cumulative sums

prod(a) % product of entries of a

cumprod(a) % cumulative product vector

min(r)

max(r)

mean(r)

median(r)

sort(r) % default = ascending order 
sort(r,'descend')

find(r>0.5) % indices where r>0.5
r(find(r>0.5)) % extract those values,
               % but not a good way
               
r>0.5 % logical array
r(r>0.5) %also extract values>0.5, but better

any(r>0.5) % if any entry > 0.5

all(r>0.5) % if all entries > 0.5

%% two vectors

a+b

x-y

a*x      % row times column = scalar
dot(a,x) % dot product
dot(x,a)

x*a  % column times row = matrix

a.*b % entrywise product

sum(a.*b) % = dot(a,b)

%% cell array

a = {'math', 253, zeros(3,1), rand(2,2)}

length(a)

a{1} 
a{2} 
a{3} 
a{4} 

b = cell(1,5) % empty cell array
b{1}
b{2} = ones(2,2)