Miscellaneous‎ > ‎

Code (details)

K-means Clustering
k-means clustering is a method of vector quantization, originally from signal processing, that is popular for cluster analysis in data mining. k-means clustering aims to partition n observations into k clusters in which each observation belongs to the cluster with the nearest mean, serving as a prototype of the cluster. This results in a partitioning of the data space into Voronoi cells.

Download

Github
              K-means

Usage
MATLAB - Unzip the file and use the test function as follows:
>>test_kmeans

Python / MATLAB
Clone the repository:
>> git clone git@github.com:rezaahmadzadeh/K-means.git

Python 
>>python k-means.py



Expectation-Maximization Algorithm
The Expectation–Maximization (EM) algorithm is an iterative method to find maximum likelihood or maximum a posteriori (MAP) estimates of parameters in statistical models, where the model depends on unobserved latent variables. The EM iteration alternates between performing an expectation (E) step, which creates a function for the expectation of the log-likelihood evaluated using the current estimate for the parameters, and a maximization (M) step, which computes parameters maximizing the expected log-likelihood found on the E step. These parameter estimates are then used to determine the distribution of the latent variables in the next E step.

Download

Usage
Unzip the file and use the test function as follows:
>>test_em

Github
              Expectation-Maximization
Usage
clone the repository:
>> git clone git@github.com:rezaahmadzadeh/Expectation-Maximization.git


Ramer-Douglas-Peucker Algorithm
The Ramer-Douglas-Peucker algorithm is a line-simplification algorithm. Given a curve specified with N points and an approximation tolerance, the algorithm selects a number of points M (M<=N) that approximate the original curve with the given tolerance. The Ramer–Douglas–Peucker algorithm (RDP) is an algorithm for reducing the number of points in a curve that is approximated by a series of points. This algorithm is also known under the names Douglas–Peucker algorithm, iterative end-point fit algorithm, and split-and-merge algorithm.

Download
Usage
Unzip the file and use the function as follows:
>>testDouglasPeucker


Collatz Conjecture
The conjecture can be summarized as follows. Take any positive integer n. If n is even, divide it by 2 to get n / 2. If n is odd, multiply it by 3 and add 1 to obtain 3n + 1. Repeat the process (which has been called "Half Or Triple Plus One", or HOTPO) indefinitely. The conjecture is that no matter what number you start with, you will always eventually reach 1.

Download
Usage
Unzip the file and use the function as follows:
>>collatz(7)
>>checkCollatz(2:1000)
            Use the 'collatz.m' to check a single number. And use the 'checkCollatz.m' to check a set of numbers.

When a function was released by MATLAB
This is a handy function which MATLAB should have done it before. It returns the version of MATLAB that the input function was released with.

Download
Usage
Unzip the file and use the function as follows:
>>when('rand')
>> ##[rand] is a built-in Matlab function (Introduced before 2006a).


Boyer-Moore Majority Vote Algorithm
The Boyer-Moore Vote Algorithm solves the majority vote problem in linear time O(n) and logarithmic space O(\log n).

Download
Usage
Unzip the file and use the function for instance
>> MajorityVote('aaaccbbcccbcc')


Visuospatial Skill Learning (VSL) (new)
A demo of Visuospatial Skill Learning approach

Usage
Clone the repository on your machine
>> demonstration.m
>> reproduction.m


Global Image Threshold
Computes the global threshold level using Otsu's method

Download
Usage
Unzip the file and use the function for instance:
>> otsu(imhist(Igray),numel(Igray))



Sorting Algorithms
Simple Sorting Algorithms including:
1- Bubble Sort
2- Cocktail Sort
3- Odd-Even Sort
4- Insertion Sort
Download
Unzip the file and use the algorithms for instance:
>> A = rand(1,100);
>> SortedA = bubbleSort(A);


Toolbox Check in MATLAB
Check to see if a specific toolbox is installed.
Download
Usage
Unzip the file and run isToolboxAvailable.m in MATLAB, for instance
>> result = isToolboxAvailable('Image Processing Toolbox','warning')
>> result =
>>    1


Stabilized Gram-Schmidt Orthonormal Method

This function receives a set of linearly independent vectors and converts them to a set of orthonormal vectors.

Download
Unzip the file and run GramSchmidt.m in MATLAB, for instance:
>> GramSchmidth([3 2;1 2])
>>ans =
 0.9487 -0.3162
 0.3162 0.9487


Function Approximation using Radial Basis Networks

In order to approximate a function defined by a set of data-points, this code utilizes a Radial Basis Network (RBN). The RBN consists of radial basis neurons in the hidden layer and linear neurons in the output layer. The weights and biases of each neuron in the hidden layer define the position and width of a radial basis function. With the correct weight and bias values for each layer, and enough hidden neurons, a radial basis network can fit any function with any desired accuracy. In order to use this function approximation in a closed-loop, in each iteration a number of new data-points are added to the previous data and the process of approximation (i.e., training the network) is repeated.

Download
Function approximation using radial basis networks [MATLAB]
Usage
Unzip the file and run:
>>faRBN.m




Particle Swarm Optimization (PSO) Algorithm (Vectorized) 

This program is a simple but vectorized implementation of the Particle Swarm Optimization in MATLAB. Particle swarm optimization (PSO) is a computational method that optimizes a problem by iteratively trying to improve a candidate solution with regard to a given measure of quality. PSO optimizes a problem by having a population of candidate solutions, here called particles, and moving these particles around in the search-space according to simple mathematical formula over the particle's position and velocity. Each particle's movement is influenced by its local best known position but, is also guided toward the best known positions in the search-space, which are updated as better positions are found by other particles. This strategy is expected to move the swarm toward the best solutions.

Download

Usage
Unzip the file and run
>>vpso.m


Van Valedhuizen's Test Suite Problems for Multi-objective Optimization

The package includes 7 standard functions of the Van Valedhuizen's test suite. These functions are one of the benchmarks for testing multi-objective optimization algorithms. A Multi-objective function is a function that has more than one objective to be optimized. 
The functions are written in vectorized form that helps the algorithm with more performance and speed. All the functions are written for MINIMIZATION except for MOP3 that is written for MAXIMIZATION. 

Download
Usage
unzip the file and you can run each function with an input in MATLAB, for instance:
>> X=rand(3,2); 
>> J=MOP7(X)


Model-based Policy Iteration Algorithm

This code is a very simple implementation of a model-based policy iteration algorithm applied to a deterministic cleaning robot problem, which makes it a useful start point for beginners in the field of Reinforcement learning and dynamic programming. The deterministic cleaning-robot has to collect a used can and recharge its batteries. the state describes the position of the robot and the action describes the direction of motion. The robot can move to the left or to the right. The first and the final states are the terminal states. The goal is to find an optimal policy that maximizes the return from any initial state.

Download

Usage
Unzip the file and run 
>>model_based_deterministic_policy_iteration.m


Q-learning (Model-free Value Iteration Algorithm) with ε-greedy Exploration

Q-learning with epsilon-greedy exploration algorithm for a deterministic cleaning robot. The deterministic cleaning robot has to collect a used can and recharge its batteries. The state describes the position of the robot and the action describes the direction of motion. The robot can move to the left or to the right. The first and the final states are the terminal states. The goal is to find an optimal policy that maximizes the return from any initial state. Here the Q-learning epsilon-greedy exploration algorithm (in Reinforcement learning) is implemented in MATLAB.

Download

Usage
Unzip the file and run
>>qlearning.m


Model-based Value Iteration Algorithm (Stochastic)

Model-based value iteration Algorithm for Stochastic Cleaning Robot. This code is a very simple implementation of a value iteration algorithm, which makes it a useful start point for beginners in the field of Reinforcement learning and dynamic programming. The stochastic cleaning-robot has to collect a used can and recharge its batteries. The state describes the position of the robot and the action describes the direction of motion. The robot can move to the left or to the right. The first and the final states are the terminal states. The goal is to find an optimal policy that maximizes the return from any initial state. Here the Q-iteration (model-based value iteration DP).

Download

Usage
Unzip the file and run
>>stochastic_robot_cleaning_v1.m


Model-based Value Iteration Algorithm (Deterministic)

Model-based value iteration Algorithm for Deterministic Cleaning Robot. This code is a very simple implementation of a value iteration algorithm, which makes it a useful start point for beginners in the field of Reinforcement learning and dynamic programming. The deterministic cleaning-robot has to collect a used can and recharge its batteries. The state describes the position of the robot and the action describes the direction of motion. The robot can move to the left or to the right. The first and the final states are the terminal states. The goal is to find an optimal policy that maximizes the return from any initial state. Here the Q-iteration (model-based value iteration DP).

Download

Usage
Unzip the file and run: 
>>deterministic_robot_cleaning_v1.m


Homogeneous 3D Transformation

This function is used to perform transformation in 3D space. The inputs to this function are the rotation angles and the translation values (roll : rotation about x axis (rad), pitch : rotation about y axis (rad), yaw : rotation about z axis (rad), translation values: tx, ty, tz, sequence : is the sequence of the rotation in an string e.g. 'xyz'). The outputs are the total homogeneous 3d transformation matrix and individual matrices (Rx : rotation matrix around x axis, Ry : rotation matrix around y axis, Rz : rotation matrix around z axis).

Download

Usage
Download the file and use the calculateTransformationMatrix function in MATLAB as follows:
>> [T,Transl,Rx,Ry,Rz] = calculateTransformationMatrix(roll,pitch,yaw,tx,ty,tz,sequence)


Trajectory Generation (3rd and 5th order)

In this package there are two functions that can be used to create smooth 3rd order and 5th order trajectories. Provide boundary conditions for the functions, the output vector can be used as a polynomial vector in MATLAB. There is also a test file that shows how to use the two functions.

Download

Usage
Unzip the file and run test.m in MATLAB to see a demo


Edit Distance Algorithm - A Standard Dynamic Programming

Edit Distance is a standard Dynamic Programming problem. Given two strings s1 and s2, the distance between s1 and s2 is the minimum number of operations required to convert string s1 to s2. The following operations are typically used: replacing one character of string by another character, deleting a character from string, adding a character to string.

Download

Usage
Unzip the file and use the EditDistance function in MATLAB as follows:
>> s1='article'
>> s2='ardipo'
>> EditDistance(s1,s2)


Random Search Algorithm (RSA)

Random search belongs to the fields of Stochastic Optimization and Global Optimization. Random search is a direct search method as it does not require derivatives to search a continuous domain. This base approach is related to techniques that provide small improvements such as Directed Random Search, and Adaptive Random Search.

Download

Usage
Unzip the file and run: 
>>RSA_visual.m


Z-N PID Controller Tuning (Zigler-Nichols)

This package can be used to tune a PID controller according to Zigler-Nichols methods. It includes two methods: method 1 uses step response and delay time and time constant parameters, and method 2 uses proportional controller action only. you should find critical gain and corresponding critical period. All results come in 2 different output shape. 
1- Controller with (Kp - Ti - Td
2- Controller with (Kp - Ki - Kd)
These methods aimed at obtaining 25% maximum overshoot in step response. So the Z-N method just finds an initial PID Controller coefficients. For a better controller you have to optimize the coefficients using other existing methods.

Download

Usage
Unzip the file and run:
>>pidtune1.m



Note: All the source-codes are free to use. You may modify it as you want. The author does not have any responsibility for any consequence of using these codes. Contact the author if you encountered any problem. Please provide references to the authors' publications in works using part of these codes.