#include "mex.h"
#include "matrix.h"
// This is a MEX-file for MATLAB. JTS 19.9.2007
// REPMAT C++-function.
// Creates from matrix y[mF:mL][nL] and numbers repm,repn
// a larger result matrix z[(mL-mF+1)*repm][nL*repn]
void repmatcf6( mwSize mF, mwSize mL, mwSize nL, mwSize
repm, mwSize repn,const double *y, double *z )
{
mwSize i1,i2,j1,j2,count=0;
//for (count=0; count<(mL-mF+1)*repm*nL*repn; count++) {
for (i2=0; i2<repn ; i2++) {
for (i1=0; i1<repm ; i1++) {
for (j2=0; j2<nL ; j2++) {
for (j1=mF-1; j1<mL ; j1++) {
*(z+count)= *(y+j1+j2*(mL-mF+1));
//*(z+count)= *(y+j1*nL+j2);
count++;
}
}
}
}
}
/* the gateway function */
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
double *y,*z;
int mrowf,mrowl;
mwSize repm,repn;
mwSize mzrows,nzcols,ncols;
/* check for proper number of arguments */
/* NOTE: You do not need an else statement when using
mexErrMsgTxt
within an if statement, because it will never get to
the else
statement if mexErrMsgTxt is executed. (mexErrMsgTxt
breaks you out of
the MEX-file) */
if(nrhs!=5)
mexErrMsgTxt("5 inputs required.");
if(nlhs!=1)
mexErrMsgTxt("One output required.");
/* get the scalar input mf,ml,nl,repm,repn */
mrowf = (int)mxGetScalar(prhs[0]);
mrowl = (int)mxGetScalar(prhs[1]);
repm = mxGetScalar(prhs[2]);
repn = mxGetScalar(prhs[3]);
/* create a pointer to the input matrix y */
y = mxGetPr(prhs[4]);
/* get the dimensions of the matrix input y */
// mrows = mxGetM(prhs[4]);
ncols = mxGetN(prhs[4]);
mzrows = (mrowl-mrowf+1)* repm;
nzcols = ncols * repn;
/* set the output pointer to the output matrix z[(mL-
mF+1)*repm][nL*repn] */
plhs[0] = mxCreateDoubleMatrix(mzrows,nzcols, mxREAL);
/* create a C pointer to a copy of the output matrix */
z = mxGetPr(plhs[0]);
/* call the C subroutine */
repmatcf6(mrowf,mrowl,ncols, repm,repn, y,z);
}
And the output:
>> j=[1,2,3;4,5,6]
j =
1 2 3
4 5 6
>> z=repmatcf6(1, 2, 1,1,j)
z =
1 2 3
4 5 6
>> z=repmatcf6(1, 2, 1,2,j)
z =
1 2 3 1 2 3
4 5 6 4 5 6
>> z=repmatcf6(1, 2, 2,1,j)
z =
1 3 2
4 6 5
2 1 3
5 4 6
Most usages of REPMAT can be replaced by a call to BSXFUN starting in
R2007a.
---Bob.
"Jozsef Molnar" <jom...@yahoo.com> wrote in message
news:fcrhus$a7n$1...@fred.mathworks.com...
My code works in Visual Basic but does not work in matlab.
Do you have any idea why?
Thanks!
J
"Bobby Cheng" <bch...@mathworks.com> wrote in message
<fcrmrh$9br$1...@fred.mathworks.com>...
Can you post to this newsgroup the MATLAB code you want to turned into MEX?
Lastly, I am not sure why your code does not work. But looking at the
output, it seems that you are using row based storage in C instead of column
base storage that MATLAB uses.
---Bob.
"Jozsef Molnar" <jom...@yahoo.com> wrote in message
news:fcrrnt$948$1...@fred.mathworks.com...
for i=1:hhs
k=hhspan(i,4);
negloglik=negloglik-msl_halt5(repmat(random((i-1)
*randdraws+1:i*randdraws,:),k,1),i,i);
end
and the function:
function[sumlik]=msl_halt5(random2,starthh,finhh)
%this function evaluates the simulated likelihood of
choices in data based
%on a parameter matrix for state variables theta and random
draws for each
%household/choice
global hhspan choicedata states randdraws hhs theta beta
charact
%compute probability of each choice
choicesmsl=size(choicedata);
span=hhspan(finhh,3)-hhspan(starthh,2)+1;
nohh=finhh-starthh+1;
t=hhspan(finhh,4); % #of weeks of all these HHs
stateutil=[zeros(span,1) states(hhspan(starthh,2):hhspan
(finhh,3),:)*theta'+[ones(span,1) charact(hhspan
(starthh,2):hhspan(finhh,3),:)]*beta'];
stateutil=kron(stateutil,ones(randdraws,1));
util=stateutil+[zeros(randdraws*nohh*t,1) random2];
temp2=exp(util);
temp=repmat(sum(temp2')',1,choicesmsl(1,2));
lnpi=log(temp2./temp)/randdraws;
lnpi1=lnpi.*kron(choicedata(hhspan(starthh,2):hhspan
(finhh,3),:),ones(randdraws,1)); %probability of actual
choices
sumlik=sum(sum(lnpi1(:,:)));
"Bobby Cheng" <bch...@mathworks.com> wrote in message
<fcuiop$9vv$1...@fred.mathworks.com>...