2

I'm looking to replicate the repmat function in R. This is the matlab code:

function prob = poissonMixturePdf(scores,lambda,mu,p)

prob = zeros(size(scores));

% pre-compute X, Y, Z probabilities

xs = 0 : floor(max(scores)/5);
px = stats.poissonpdf(xs,lambda);

ys = 0 : floor(max(scores)/3);
py = stats.poissonpdf(ys,mu);

zs = 0 : floor(max(xs));
pz = binopdf(repmat(xs,length(zs),1),repmat(zs',1,length(xs)),p);

And this is my R code:

prob<-rep(0,scores)


xs<-0:floor(max(scores)/5)
px<-dpois(xs,lambda)

ys<-0:floor(max(scores)/3)
py<-dpois(ys,mu) 

zs<-0:floor(max(xs))
pz<- dbinom(matrix(rep(xs,times=1),nrow=length(zs)),matrix(rep(t(zs),times=length(xs)),1),p)

Is the last line correct? I dont have Matlab, just a script.

clattenburg cake
  • 1,096
  • 3
  • 19
  • 40

1 Answers1

4

The equivalent of Matlab's repmat(a,2,3) in base R is kronecker(matrix(1,2,3),a).

There's also R functions repmat identical in usage to Matlab's in the R packages matlab and pracma. You can look at their source code if you like.

halfer
  • 19,824
  • 17
  • 99
  • 186
Tom Wenseleers
  • 7,535
  • 7
  • 63
  • 103