MATLAB chunk of code for jj = 1:numOfAA curAAPosInSeqIndex = aaPosInSeqIndex{1,jj}; for kk = 1:K p_c_a_z_given_x_contribution(1,jj,kk) = sum(sum(p_c_a_z_given_m_x_permuted(:,:,kk) .* curAAPosInSeqIndex, 1),2); % 1 by numOfAA by K end end aaPosInSeqIndex is a list of size 24, with each element a vector. p_c_a_z_given_m_x_permuted is an array of size N=1655 by J=39 by K=151 and p_c_a_z_given_x_contribution is an array of size 1 by numOfAA = 24 by K=151 R equivalent:app = t(sapply(1:numOfAA, function(xx) (t(sapply(1:K, function(x) colSums(p_c_a_z_given_m_x_permuted[,,x]* aaPosInSeqIndex[[xx]])))[,3]))) p_c_a_z_given_x_contribution = app I would be very grateful if someone could look at the R code and let me know if there is something missing or if there is a better way of getting the exact MATLAB output uing R code i.e. could someone write a code that would do what the MATLAB chunk of code above is doing. The R code does not output the same MATLAB output. Thanks in advance for your help.JN [[alternative HTML version deleted]]