Q:

Translating a function from R into Matlab

I am trying to reproduce a function in R in Matlab. I do not have much experience with R so it is difficult for me to understand some of the code. Here is the R code:
# Model Calculations
# ==================
#
# Function to determine length of stay df.
#
# t = time since hospital admission in days
# age = age at admision in years
# type = type of stroke (1. Haemorhagic, 2. Cerebral Infarction, 3. TIA)
#
los.df <- Vectorize(function(t, age, type,dest){
    par <- c(6.63570, -0.03652, -3.06931,  0.07153, -8.66118,
    0.08801, 22.10156, 2.48820, 1.56162, 1.27849,
    11.76860, 3.41989, 63.92514)
    alpha1 <- par[1]
    beta1 <- par[2]
    alpha2 <- par[3]
    beta2 <- par[4]
    theta0 <- par[5]
    theta1 <- par[6]
    mu1 <- par[7]
    mu2 <- par[8]
    mu3 <- par[9]
    mu4 <- 0
    nu1 <- 0
    nu2 <- 0
    nu3 <- par[10]
    nu4 <- 0
    rho1 <- 0
    rho2 <- par[11]
    rho3 <- par[12]
    rho4 <- par[13]
    #
    p <- exp(-exp(theta0 + theta1*age))
    temp.mat <- matrix(c(1,0,1,0,1-p,p),3,2,byrow=TRUE)
    initial.state.vec <- rep(0,7)
    initial.state.vec[c(type,type+1)] <- temp.mat[type,]
    #
    lambda1 <- exp(alpha1 + beta1*age)
    lambda2 <- exp(alpha2 + beta2*age)
    Q <- matrix(0,7,7)
    Q[1,] <- c(-(lambda1+mu1+nu1+rho1), lambda1, 0, 0, mu1, nu1, rho1)
    Q[2,] <- c(0, -(lambda2+mu2+nu2+rho2), lambda2, 0, mu2, nu2, rho2)
    Q[3,] <- c(0, 0, -(mu3+nu3+rho3), 0, mu3, nu3, rho3)
    Q[4,] <- c(0, 0, 0, -(mu4+nu4+rho4), mu4, nu4, rho4)
    Pt <- expm(t/365*Q)
    Ft <- sum(as.numeric(initial.state.vec %*% Pt)[dest:dest])
    return(Ft)
})

I am having difficulty in understanding the following lines of code and what they mean:
    temp.mat <- matrix(c(1,0,1,0,1-p,p),3,2,byrow=TRUE)
     initial.state.vec <- rep(0,7)
     initial.state.vec[c(type,type+1)] <- temp.mat[type,]
Here is my Matlab code in which I have tried to reproduce the R code:
function Ft = losdf(age, strokeType, dest)

% function to determine length of stay in hospitaal of stroke patients
% t = time since admission (days);
% age = age of patient;
% strokeType = 1. Haemorhagic, 2. Cerebral Infarction, 3. TIA;
% dest = 5.Death 6.Nursing Home 7. Usual Residence;

alpha1 = 6.63570;
beta1 = -0.03652;
alpha2 = -3.06931;
beta2 = 0.07153;
theta0 = -8.66118; 
theta1 = 0.08801;
mu1 = 22.10156;
mu2 = 2.48820;
mu3 = 1.56162;
mu4 = 0;
nu1 = 0;
nu2 = 0;
nu3 = 1.27849;
nu4 = 0;
rho1 = 0;
rho2 = 11.76860;
rho3 = 3.41989;
rho4 = 63.92514;

Ft = zeros(365,1);
for t = 1:1:365
p = (exp(-exp(theta0 + (theta1.*age))));

if  strokeType == 1
    initialstatevec = [1 0 0 0 0 0 0];
elseif strokeType == 2
    initialstatevec = [0 1 0 0 0 0 0];
else
    initialstatevec = [0 0 (1-p) p 0 0 0];
end

lambda1 = exp(alpha1 + (beta1.*age));
lambda2 = exp(alpha2 + (beta2.*age));

Q = [ -(lambda1+mu1+nu1+rho1) lambda1  0  0  mu1  nu1  rho1; 
0  -(lambda2+mu2+nu2+rho2) lambda2 0 mu2 nu2 rho2; 
0 0 -(mu3+nu3+rho3) 0 mu3 nu3 rho3; 
0 0 0 -(mu4+nu4+rho4) mu4 nu4 rho4; 
0 0 0 0 0 0 0; 
0 0 0 0 0 0 0; 
0 0 0 0 0 0 0];

Pt = expm(t./365.*Q);
Pt = Pt(strokeType, dest);
Ft(t) = sum(initialstatevec.*Pt);

end

When I plot the output, it is not giving me the same values in Matlab as it is in R. I cannot identify where I am going wrong; I know my values of Pt are correct. I think I may have made an error in setting up initialstatevec or in defining Ft(t)?
If anyone can give me advice on where I have went wrong this would be much appreciated.

A:

This line temp.mat <- matrix(c(1,0,1,0,1-p,p),3,2,byrow=TRUE) creates a matrix of 3 rows and 2 columns and are putting the elements you give him by filling rows by rows. 
Let's say p = 0.5 and temp.mat is like this :
     [,1] [,2]
[1,]  1.0  0.0
[2,]  1.0  0.0
[3,]  0.5  0.5

This line initial.state.vec <- rep(0,7) creates a vector of seven 0 :
> initial.state.vec
[1] 0 0 0 0 0 0 0

And this line initial.state.vec[c(type,type+1)] <- temp.mat[type,], according to the value of type : 1, 2 or 3, puts the row indexed by type of your matrix in specific indexes of vector which are also calculated according to the value of type. It means that :
For type = 1, you take the 1st row of your matrix and you put in the 1st and 2nd index of your vector. For type = 2, you take the 2nd row of your matrix and you put it in the index 2 and 3 and for type = 3, you take the 3rd row and you put in the index 3 and 4.
Example with type = 3 : 
temp.mat[type,]
[1] 0.5 0.5

initial.state.vec
[1] 0.0 0.0 0.5 0.5 0.0 0.0 0.0

Hope it will help for your translation.

