how to plot a 2D gaussian in a graph using surf

4 Ansichten (letzte 30 Tage)
Gadadhar Sahoo
Gadadhar Sahoo am 13 Mär. 2018
Bearbeitet: Birdman am 13 Mär. 2018
clear
clc
load (fullfile('C:\Users\CHINMAYEE\Desktop\Datasets\red_data.txt'))
size(red_data)
[N, M] = size(red_data);
X = red_data(:,2);
[N, M] = size(X);
max_X = max(max((X)));
min_X = min(min(X));
K = 2;
means = min_X + (max_X - min_X)*rand(K,M);
%C = cov(X);
%covs = zeros(K,M,M);
covs = cell(1, K);
for i=1 :K
covs{i}=eye(M);
end
%D = det(C);
%I = inv(D);
prior = ones(1,K)/K;
prob = zeros(N, K);
gens = 10;
for g = 1 : gens
for p = 1 : N
for k = 1 : K
prob(p,k)= gaussian2D(X(p,:),means(k,:),covs(k));
prob(p,k)= prob(p,k)* prior(k);
end
sum_probs = sum(prob(p,:));
prob(p,:) = prob(p,:)/sum_probs;
end
for k = 1 : K
means(k,:) = sum(repmat(prob(:,k),1,2).*X)/sum(prob(:,k));
for i=1:M
for j=1:M
norm_data = (X(:,i)-repmat(means(k,i),N,1)).*(X(:,j)-repmat(means(k,j),N,1));
covs{k}(i,j) = sum(prob(:,k).*norm_data)/sum(prob(:,k));
end
end
prior(k) = sum(prob(:,k))/N;
end
end
str = '';
for i = 1 : N
[s ,loc]= sort(prob(i,:));
str = strcat(str, num2str(loc(K)));
end
str
figure
scatter(X,zeros(1,N));
hold on
ax = linspace(-2, 2, 100);
y = linspace(-3, 3, 100);
[X Y] = meshgrid(x, y);
X
Y
Z = gaussian2D(ax,means(X),covs(X));
surf(x, y, Z);

Antworten (0)

Kategorien

Mehr zu Bounding Regions finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by