function intro()
% An introduction to the MatLab programming, the most useful codes
%
% http://strijov.com
% INPUT
vec = [1; 2; 3]
vecT = [1 2 3]
mat = [1 2 3
4 5 6]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% VECTORS AND MATRICES
m = 3;
n = 2;
mat1 = ones(m,n)
mat0 = zeros(m,n)
mat2 = rand(n,n)
mat4 = randn(n,n)
mat3 = rand(n,n,n)
vec = [0:0.01:1] % make a vector
vec = [0:100]
vec = linspace(0,1,100)
% concantenate 2 vectors
vec = [0:0.5:1, 2:0.4:3]
% append an element after and before
vec(end)
vec(end+1) = 3
vec = [-1 vec] % for row-vectors!
% vectorisation, align a vector dimensions
vec = mat4(:)
idx = find(vec < 0.05)
vec(idx) = []
vec=[]
mat1 = [1:5]'*[1:5]
mat2 = mat1(2:4,2:5)
mat2 = mat2'
mat2 = [mat1; mat1]
mat2 = [mat1, mat1]
% the string is a vector, too
i = 2
str = ['The lucky number is ', num2str(i)]
% but not vice versus
str = ''; % empty string
str = sprintf(' 1 plus%s %d',str,i)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% OPERATIONS
% dot production
x = [1:5]';
norm = x'*x
table = x*x'
% and matrix multiplication
A = rand(5,2)
ATA = A'*A
ATA1 = inv(ATA)
ATA1'*ATA
% element-by-element operations
y = 1+x
y = x+x
y = x.*x
Y = A.*A
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% STRUCTURES
PP.niter = 100;
PP.tolX = 10e-6;
PP.display = 1;
niter = PP(1).niter;
PP
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% CELL ARRAYS
strarray = {'one','two','et cetera'}
strarray = {'north','south';'west','east'}
fprintf(1, 'Go to the %s!\n', strarray{1,1});
% but, note that the following item is a cell
strarray(1,1)
strarray{1,1}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% FOR
% This is convenient to use a construction
x=rand(5,1); % get some x
for i=1:length(x)
fprintf(1,'%1.2f\n',x(i)); % some code uses x(i)
end
% and the following construction, too
x=rand(5,1); % get some x
idx = find(x>0.5); % find some elements of x to process
for i = idx % here idx is integer-valued vector
fprintf(1,'%1.2f\n',x(i)); % some code uses x(i)
end
% its is very good to operate like that
idx = [0.1, 0.5, 0.7]; % get some index values (real, why not?)
for i=idx
fprintf(1,'%1.2f\n',i);
end
% an note that no need to use additional lines if you deal with vectors
mat = [1 3 5 7; 2 4 6 8]
for i = mat
fprintf(1,'i'' = %s\n',num2str(i'));
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% IF
% It is convenient to use quantifiers all and any instead of login
% operations and(a,b), synonim 'a & b' and or(a,b), synonim a | b.
% Therse quantifiers get vector as a argument.
a = 1;
b = 2;
c = []; % empty
d = NaN; % Non a Number
e = -Inf; % Infinity
if any([a>b, isempty(c), isnan(d), isinf(e)])
disp('something remarkeble happens')
end
% instead of
if or (a>b, or( isempty(c), or( isnan(d), isinf(e))))
disp('something remarkeble happens');
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% CODING
% This is obvious to write an effective code. I.e. instead of
% Disjecta code 'Overdrive'
x = [0:0.01*2*pi:2*pi]; % the length of the vector is 101
y = sin(x);
y1 = y;
CONST = 0.2;
for i = 1:length(y1)
if y1(i) > CONST, % please, do not act like that!
y1(i) = CONST;
elseif y1(i) < -CONST;
y1(i) = -CONST;
end
end
plot(x,y,'r-');
hold on
plot(x,y1,'b-');
hold off
% it's better to write
% Disjecta code 'Overdrive'
n = 100;
x = linspace(0,2*pi,n);
y = sin(x);
idx = find(abs(y) > CONST);
y(idx) = sign(y(idx))*CONST; % here the same result in a few lines of code!
% It is neccessary to use embedded Matlab operations, but not construct
% cumtom-made.
% Disjecta code 'Multiply some matrix'es columns by some values'
n = 5;
x = rand(n,1);
A = magic(n);
for j = 1:n
A(:,j) = A(:,j)*x(j);
end
% It's better to do
% Disjecta code 'Multiply some matrix'es columns by some values'
n = 5;
x = rand(n,1);
A = magic(n);
A = A*diag(x);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% FUNCTIONS
% types of functions: embedded, private, public and inline.
% WARNING (after functions)
% All the fictions, especially fictions of regression models and 'mapsto'
% functions must get a scalar as well as a vector of a matrix to process.
% So if there is a scalar is an argument, a function is a scalar, and so on
% for the a vector and a matrix.
% example of the warning: normalize a vector
% place the code below in a separate file named 'to01.m'
%function y = to01(x)
% y = (x - min(x))./(max(x) - min(x))
%return
% DO NOT FORGET TO REMOVE THE COMMENTS '%' WHEN PLACED
%... and use this function like that
x = randn(1,20);
y = to01(x);
min(y)
max(y)
% function evaluation
eval('x = [1,2;3,4];')
[m,n] = eval('size(x)')
mat = feval('rand',2,2)
% how to make an [a x b x c x d x ... x e] (k-times)
k = 5; % have to generate k-dimantional array
dims=randperm(k+1) % let the dimension sizes be random
dims(find(dims==1)) = [] % remove the size of 1
str = sprintf('%d,',dims) % type the vector into a string
str = sprintf('rand(%s);',str(1:end-1)) % add the rand function
mat = eval(str); % evaluate the expression
size(mat) % check the size of the obtained multi-dimensional array mat
% inline functions
f1 = inline('w(1) + w(2)*x','w','x');
y1 = f1([1 2],3)
y1 = f1([1,2],[1:5]') % be care of this
f2 = inline('(x - min(x))./(max(x) - min(x))','x');
y2 = f2([1:10])
% THAT'S THE END OF THE EXECUTABLE CODE
return
% One easily can vary the number of input and output arguments.
function [a,b,c] = somefunc(d,e,f)
if nargin < 3, f = 1; end
% some code... , i.e.
a = d;
b = e;
if nargout > 2, c = f; end
return