Hello,
I would like to write a mata function, which chooses every other/second row respectively column of a vector and calculates the sum of these values.
I tried to select every other column/row with the command mod(), but this does not work. I am not sure how to tell the program that it must only allow for every other column/row.
Thank you for your help in advance.
I would like to write a mata function, which chooses every other/second row respectively column of a vector and calculates the sum of these values.
Code:
function test (real vector x){
real scalar i
real scalar j
real scalar r
real scalar c
r = rows(x)
c = cols(x)
if (c==1) {
if (mod(i,2):==0){
B=x[i ,1]
sum(B)
}
}
if (r==1) {
if(mod(j,2):==0) {
A=x[1,j]
sum(A)
}
}
}
Thank you for your help in advance.

Comment