Hello. I have been trying for many hours now how to successfully translate this MATLAB dot product inside a for loop into RStudio. Here is the MATLAB code:

m = 24;

k = 24;

s = 449:

yhb= zeros(s-m+k,1);

for i = 1:s-m+k

yhb(i,:)=dot(krf(7,1:i),hat(2,i:-1:1));

end

yhb is a 449 x 1 vector, krf is a 16 x 449 matrix, and hat is a 4 by 425 matrix. Here is the screenshot of the first few rows of yhb created from the for loop.

Here is the code that I first tried in RStudio:

m <- 24

k <- 24

s <- 449

yhb <- matrix(0,s-m+k,1)

for (i in 1:(s-m+k)) {

yhb[i,] <- sum(krf[7,1:i]*hat[2,rev(hat)])

}

As you can see that for me to get the dot product I use sum(vector_a * vector_b). And, from the MATLAB code which reverses the order of the elements in hat, I use the function rev(hat). I am not confident whether this is the right way, particularly, whether I can use the function rev to index/access the columns of hat.

I am then getting an error message which is this:

**Error in h(simpleError(msg, call)) : **
** error in evaluating the argument 'x' in selecting a method for function 'sum': only 0's may be mixed with negative subscripts**

I then tried to manually verify where I am getting it wrong by running these individual set of codes:

hat2 <- hat[2,]

for (i in 1:(s-m+k)) {

krf <- krf[7,1:i]

}

hat3 <- as.matrix(t(rev(hat2))

At this point, hat3 exactly replicates the hat(2,i:-1:1) inside the MATLAB for loop, so far so good. But then when I issue this command:

yhb <- as.matrix(sum(krf*hat3))

I am able to get the correct dimension of yhb but does not replicate the one produced by the MATLAB for loop above. I am stuck.

Appreciate any help you can provide, please. Thank you so much.