-
Notifications
You must be signed in to change notification settings - Fork 1
/
bayes.m
31 lines (26 loc) · 933 Bytes
/
bayes.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
function [ mu, sigma ] = bayes(D)
%BAYES Computes the mean (mu) and deviation (sigma) for D
% INPUT data matrix D(MxN)
% RETURN [mu, sigma]
% The first n-1 columns of D is the feature vector (x) and the n:th column
% is the class vector (c). This function computes mu and sigma for each of
% the n-1 vectors based on their indicator variable (c in classes)
class = D(:,end);
% Unique classes in class (Indicator variables)
classes = unique(class);
nr_classes = length(classes);
D(:,end) = []; % Remove last column
[~, n] = size(D);
% Pre allocate space
mu = zeros(nr_classes , n);
sigma = zeros(nr_classes , n);
% Calculate the mean and deviation for class c in feature vector x
for c=1:nr_classes
index = find(class == classes(c));
for i=1:n
x = D(:,i);
mu(c, i) = sum(x(index)) / length(index);
sigma(c, i) = sqrt( sum( (x(index) - mu(c,i)).^2 ) ./ length(index) );
end
end
end