how to plot a gaussian 1D in matlab

조회 수: 923 (최근 30일)
Gadadhar Sahoo
Gadadhar Sahoo 2017년 12월 1일
댓글: Chad MacDonald 2023년 8월 2일
for k = 1 : K
ax = linspace(min_x,max_x,100);
y = my_gaussian(x,means,vars);
plot(ax,y);
end

채택된 답변

M
M 2017년 12월 1일
편집: Adam Danz 2020년 7월 14일
You can use Matlab function to construct Gaussian function :
x = 0:0.1:10;
y = gaussmf(x,[2 5]);
plot(x,y)
  댓글 수: 4
Gadadhar Sahoo
Gadadhar Sahoo 2017년 12월 1일
i am not getting the gaussian bell curve..here is my code
clc clear load fisheriris [N, M] = size(meas); x = meas(:,1)'; max_x = max(max((x))); min_x = min(min(x)); K = 3; means = min_x + (max_x - min_x)*rand(1, K); vars = ones(1, K); prior = ones(1,K)/K; prob = zeros(N, K); for g = 1 : 1 for p = 1 : N for k = 1 : K gaussian = (1/sqrt(2*pi*vars(k)))*exp(-(x(p)-means(k)).^2/(2*vars(k))); prob(p,k) = gaussian* prior(k); end sum_probs = sum(prob(p,:)); prob(p,:) = prob(p,:)/sum_probs; end for k = 1 : K means(k) = sum(prob(:,k)'.*x)/N; vars(k) = sum(prob(:,k)'.*(x - means(k)).^2)/N; prior(k) = sum(prob(:,k))/N; end end figure scatter(x,zeros(1,N)); hold on for k = 1 : K ax = linspace(min_x,max_x,100); y = gaussmf(ax,[means,vars]); plot(ax,y);
end
Chad MacDonald
Chad MacDonald 2023년 8월 2일
The gaussmf function evaluates a Gaussian membership function for a fuzzy logic system, which is not the same thing as a Gaussian distribution. For more information on Gaussian probability distributions, see Normal Distribution (Statistics and Machine Learning Toolbox).

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Adam Danz
Adam Danz 2020년 7월 14일
편집: Adam Danz 2022년 6월 14일
Fully parameterized gaussian function (no toolboxes needed)
If you don't have the Fuzzy Logic toolbox (and therefore do not have access to gaussmf), here's a simple anonymous function to create a paramaterized gaussian curve.
gaus = @(x,mu,sig,amp,vo)amp*exp(-(((x-mu).^2)/(2*sig.^2)))+vo;
  • x is an array of x-values.
  • mu is the mean
  • sig is the standard deviation
  • amp is the (positive or negative)
  • vo is the vertical offset from baseline (positive or negative)
To add noise along the y-axis of the guassian,
y = gaus(___);
yh = y + randn(size(y))*amp*.10; % noise is 10% of the amp
The doc page on the Normal Distribution may also be helpful.
Demo
x = linspace(-5,25,100);
mu = 10;
sig = 5;
amp = 9;
vo = -5;
y = gaus(x,mu,sig,amp,vo);
% Plot gaussian
plot(x, y, 'b-', 'LineWidth',3)
% Add noise
yh = y + randn(size(y))*amp*.10;
hold on
plot(x, yh, 'ro','markersize', 4)
grid on
title(sprintf('Guassian with \\mu=%.1f \\sigma=%.1f amp=%.1f vo=%.1f', ...
mu, sig, amp, vo))
Comparison with gaussmf()
x = linspace(-15,10,100);
mu = -5.8;
sig = 2.5;
amp = 1;
vo = 0;
y = gaus(x,mu,sig,amp,vo);
% Plot gaussian from custom function
plot(x, y, 'b-', 'LineWidth',3, 'DisplayName','Custom function')
% Plot gaussian from custom function
y2 = gaussmf(x,[sig,mu]);
hold on
plot(x, y2, 'r--', 'LineWidth',4, 'DisplayName','gaussmf()')
grid on
title(sprintf('Guassian with \\mu=%.1f \\sigma=%.1f amp=%.1f vo=%.1f', ...
mu, sig, amp, vo))
legend()
  댓글 수: 1
Kibum Nam
Kibum Nam 2020년 8월 16일
very useful. thanks a lot.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Statistics and Machine Learning Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by