How do I change my scalars to fit my normpdf?

25 次查看(过去 30 天)
Here's my code (I am not using the normpdf function because the problem specifically asks to write out the whole equation to get the answer)
e = 2.7183;
xq = 0:100;
mu = [25, 50];
sigma = [5, 10];
figure(3)
for ii2 = 1:length(xq)
ndist=(1/(sigma*sqrt(2*pi)))*power(e,-(1/2)*(power(xq-mu)/sigma),2)
subplot (2,1,ii2)
bar(ndist)
end
  2 个评论
Torsten
Torsten 2024-9-9,20:56
Please explain what you are trying to do. I can't deduce it from your errorneous code.
Leyton
Leyton 2024-9-9,21:00
I am trying to plot 2 normal distributions using the parameters above the for loop

请先登录,再进行评论。

采纳的回答

Torsten
Torsten 2024-9-9,21:15
编辑:Torsten 2024-9-9,21:16
xq = 0:100;
mu = [25, 50].';
sigma = [5, 10].';
ndist = 1./(sigma*sqrt(2*pi)).*exp(-((xq-mu)./sigma).^2);
plot(xq,ndist)
grid on
  2 个评论
Leyton
Leyton 2024-9-9,21:20
Thank you so much! This is much cleaner than what I typed for sure
Torsten
Torsten 2024-9-9,21:33
编辑:Torsten 2024-9-9,21:36
Note that "mu" and "sigma" are column vectors and "xq" is a row vector.
Thus "pdist" will come out as a matrix:
xq = [1 2 3];
mu = [3 4].';
sigma = [2 9].';
(xq - mu)./sigma
ans = 2x3
-1.0000 -0.5000 0 -0.3333 -0.2222 -0.1111
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
This is already "advanced" MATLAB. Alternatively, you should try to produce "pdist" using a loop.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Introduction to Installation and Licensing 的更多信息

标签

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by