Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab 当从二元正态分布生成1000个随机数时,绘制一个二元pdf_Matlab_Plot_Matlab Figure_Normal Distribution - Fatal编程技术网

Matlab 当从二元正态分布生成1000个随机数时,绘制一个二元pdf

Matlab 当从二元正态分布生成1000个随机数时,绘制一个二元pdf,matlab,plot,matlab-figure,normal-distribution,Matlab,Plot,Matlab Figure,Normal Distribution,我需要通过从给定的(mu和sigma)双变量正态分布生成1000个随机变量来绘制双变量Pdf。我试着写一个代码(Matlab),但有一些错误。谁能检查一下,帮我改正一下吗 function [ ] = bivariatedn( mu,sigma) % Generating 1000 random variables R=mvnrnd(mu,sigma,1000); X=R(:,1); Y=R(:,2); % Define the grid for visualization

我需要通过从给定的(
mu
sigma
)双变量正态分布生成1000个随机变量来绘制双变量Pdf。我试着写一个代码(Matlab),但有一些错误。谁能检查一下,帮我改正一下吗

function [ ] = bivariatedn( mu,sigma)
  % Generating 1000 random variables
  R=mvnrnd(mu,sigma,1000);
  X=R(:,1);
  Y=R(:,2);
  % Define the grid for visualization
  [X,Y]=meshgrid(R(:,1),R(:,2));

  % Define the constant (To write pdf in a easy way)
  const = (1/sqrt(2*pi))^2;
  const = const/sqrt(det(sigma));
  temp = [X(:)-mu(1) Y(:)-mu(2)];

  pdf=zeros(length(X),1);

  d=zeros(length(X),1);
  d=diag(temp*inv(sigma)*temp');
  for i=1:length(X)
    pdf(i)= const*exp(-0.5*d(i));
  end
  % plot the result
  surfc(X, Y, pdf, 'LineStyle', 'none');
end
使用surfc时出错(第39行) 曲面Z必须包含多个行或列


这是正在显示的错误。有人可以研究一下吗?

问题正是错误消息所说的,曲面Z(变量
pdf
)只包含一列,它必须包含多行/column@SardarUsama那么我应该删除只返回对角线元素的diag函数吗?