edgarmtze
edgarmtze

Reputation: 25058

R code to MATLAB

Here is a code in R, I would like to run it with MATLAB, optimizing if possible.

require(lattice)
#
set.seed(42)

# parameters for distribution of class 1
a <- 0
b <- 1
# parameters for distribution of class 2
c <- 3
d <- 1

x <- c(sort(rnorm(1000,mean=a,sd=b)),sort(c(rnorm(1000,mean=c,sd=d))))
y <- c(dnorm(x[1:1000],mean=a,sd=b),dnorm(x[1001:2000],mean=c,sd=d))
labels <- factor(rep(c("class 1","class 2"),each=1000))

dat <- data.frame("x"=x,"density"=y,"groups"=labels)
xyplot(density~x,data=dat,groups=labels,type="b",auto.key=T)

Upvotes: 0

Views: 1099

Answers (1)

abcd
abcd

Reputation: 42245

Looks like you're plotting two normal distributions. Here's the equivalent in matlab.

mu1=0;sigma1=1;   %# parameters for the first distribution
mu2=3;sigma2=1;   %# parameters for the second distribution

%# get density at 1000 points equally spaced between mu-4sigma & mu+4sigma for each class
x1=linspace(mu1-4*sigma1,mu1+4*sigma1,1000);
p1=normpdf(x1,mu1,sigma1);  

x2=linspace(mu2-4*sigma2,mu2+4*sigma2,1000);
p2=normpdf(x2,mu2,sigma2);

%# plot the densities.
plot(x1,p1,x2,p2)

Upvotes: 1

Related Questions