Recitation Algorithms
function [z] = ReverseVector( x )
% x: vector of length n
% z: vector of length n, obtained by reversing the order of the components
% of x
n=length(x);
for i=1:n
z(i)=x(n+1-i);
end
Alternatively
n=length(x);
z=x(n:-1:1);
function y = myFactorial(n)
% computes y = n!
y = 1;
for i=2:n
y = y*i;
end
Alternatively
function y = myFactorial(n)
x = 1:n;
y = prod(x);
Alternatively (recursive version)
function y = myFactorial(n)
if n == 1
y = 1;
else
y = myFactorial(n-1)*n;
end
function [t B] = SignedBinaryInt(b)
% b = [b1 , b2 , b3, b4] is a row vector of 4 binary bits
% t = b(1) is the sign of a binary integer B
% B is the binary integer +b2b3b4 0r b2b3b4
t = b(1);
B = b(2:4);
if t == 1
B=[ = ', num2str(B)];
elseif t == 0
B=[ + ', num2str(B)];
end
function [y] = MyPolyValStraightScalar(a,x)
% Inputs: a is a vector of length n+1
%
x is a scalar
% Outputs: y= a(1)+a(2)*x+...+a(n+1)*x^n is a number
y=0;
term = 1;
m=length(a);
for i= 1:m
y=y+a(i)*term;
term=term*x;
end
function [y] = MyPolyValStraightVector(a,x)
% Inputs: a is a vector of length n+1
%
x is a vector of length k
% Outputs: y= a(1)+a(2)*x+...+a(n+1)*x^n is a vector of length k that.
evaluates the polynomial at each component of x.
x=x(:);
k=length(x);
y=zeros(k,1);
term = ones(k,1);
m=length(a);
for i= 1:m
y=y+a(i)*term;
term=term.*x;
end