0

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)
1
  • 1
    Adding more documentation as to what the different parts do would help. There are people who know MatLab, but not R. With these comments they should still be able to help. Commented Mar 12, 2011 at 16:35

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.