Matlab - Object-oriented programming

From XennisWiki
Jump to: navigation, search

Use object-oriented programming in Matlab

Basic example

Define a class

classdef Car
    properties
        color;
        hp;
    end
    
    methods
        function obj = Car()
            obj.color = 'red';
            obj.brand = 25;
        end

        function obj = setColor(obj, color)
            obj.color = color;
        end

        function hp = getHp(obj)
            hp = obj.hp;
        end
    end % .methods
end % .classdef

Usage

myCar = Car();

myCar.color = 'green';
myCar.setColor('blue');

myCar.color % Outputs 'blue'
myCar.getHp() % outputs 25

Usage in arrays

numCars = 10;
cars = cells(1, numCars);
for i=i:numCars
    cars{i} = Car();
end

Advanced example

Concept.m

classdef Concept
% This class defines a concept c element in concept class C.
%
	properties
		xMin;
		xMax;
		yMin;
		yMax;
	end
	
	methods
		% Create a rancom goal concept
		function obj = Concept()
			% compute xMin and xMax
			x1 = 100*rand(1);
			x2 = 100*rand(1);
			if (x1 < x2)
				obj.xMin = x1;
				obj.xMax = x2;
			else
				obj.xMin = x2;
				obj.xMax = x1;
			end
			
			% compute yMin and yMax
			y1 = 100*rand(1);
			y2 = 100*rand(1);
			if (y1 < y2)
				obj.yMin = y1;
				obj.yMax = y2;
			else
				obj.yMin = y2;
				obj.yMax = y1;
			end
		end
		
		% Ask the oracle, if the given sample is in this concept or not
		function result = askOracle(obj, sample)
			checkX = sample.x >= obj.xMin && sample.x <= obj.xMax;
			checkY = sample.y >= obj.yMin && sample.y <= obj.yMax;
			result = checkX && checkY;
		end
	end % methods
end % classdef

Hypothesis.m

classdef Hypothesis
% This class defines a hypthesis h.
%
	properties
		xMin;
		xMax;
		yMin;
		yMax;
	end
	
	methods
		% Creates an hypothesis
		function obj = Hypothesis()
			obj.xMin = 100;
			obj.yMin = 100;
			obj.xMax = 0;
			obj.yMax = 0;
		end
		
		% Learns by using the given sample and it's label
		function obj = learn(obj, sample, isPositive)
			% Only learn, if it's a positive sample
			if isPositive
				% Update xMin and xMax
				obj.xMin = min(obj.xMin, sample.x);
				obj.xMax = max(obj.xMax, sample.x);
				% Update yMin and yMax
				obj.yMin = min(obj.yMin, sample.y);
				obj.yMax = max(obj.yMax, sample.y);			
			end
		end
		
		% Computes the error inbetween the hypothesis and the given concept
		function error = getError(obj, concept)
			conceptSize = (concept.xMax-concept.xMin) * (concept.yMax-concept.yMin);
			
			if obj.xMax >= obj.xMin
				hypothesisSize = (obj.xMax-obj.xMin) * (obj.yMax-obj.yMin);
			else
				hypothesisSize = 0;
			end
			
			error = (conceptSize - hypothesisSize)  / (100 * 100);
			
			assert(error >= 0, 'Error should be >= 0, it is %g', error);
			assert(error <= 1, 'Error should be <= 1, it is %g', error);
		end
	end % methods
end % classdef

Sample.m

classdef Sample
% Class defines a sample s element in learn domain X
%
	properties
		x;
		y;
	end
	
	methods
		% Creates an random sample
		function obj = Sample()
			obj.x = 100 * rand(1);
			obj.y = 100 * rand(1);
		end
	end % methods
end % classdef

main.m

clc
clear all

% Initialization ----------------------------------------------------------
numConcepts = 1000;
numSamples = 100;
delta = 0.05;
taus = zeros(1, numSamples);
epsilons = zeros(1, numSamples);

% Create random concepts and empty hypotheses -----------------------------
concets = cell(1, numConcepts);
hypotheses = cell(1, numConcepts);
for i=1:numConcepts
	concets{i} = Concept();
	hypotheses{i} = Hypothesis();
end

% Iterate samples ---------------------------------------------------------
for i=1:numSamples
	sample = Sample(); % random sample
	errors = zeros(1, numConcepts);
	
	for j=1:numConcepts
		concept = concets{j};
		hypothesis = hypotheses{j};
		
		hypotheses{j} = hypothesis.learn(sample, concept.askOracle(sample));
		errors(j) = hypothesis.getError(concept);
	end

	errors = sort(errors); % sort errors from low to high
	index = floor(0.95 * numConcepts); % tau(s) >= 95%
	
	taus(i) = errors(index);
	epsilons(i) = 4/i * log(4/delta);
	
	%fprintf('%u:\ttau=%f,\tepsilon=%f\n', i, taus(i), epsilons(i));
end

% Plot --------------------------------------------------------------------
figure(1)
	plot(taus, 'b')
	hold on
	plot(epsilons, 'r')
	hold off
	grid on
	legend('\tau(s)', '\epsilon(s) = 4/\epsilon ln 4/\delta')
	xlabel('Sample size s')
	ylabel('Error rate at 95%')
	axis([0 numSamples 0 1])

See also