Dear Statalists,
For an assignment I need to write a Mata program that recognises the kind of input and either runs the function for a Mata matrix or for the Stata variable that is specified in the function.
Here you see the (simplified) program, that only works for Mata matrix:
Here you see the (simplified) program, that only works for Stata variable:
Now I need one Mata program that either works for a Mata matrix or for a Stata variable. I tried several things but nothing worked. Therefore, I need your help!
Thank you very much in advance!
Best wishes
Anika
For an assignment I need to write a Mata program that recognises the kind of input and either runs the function for a Mata matrix or for the Stata variable that is specified in the function.
Here you see the (simplified) program, that only works for Mata matrix:
Code:
clear mata
mata
real matrix function posmean (real vector x) {
real vector A, B
A = select(x, x[1,.]:>0)
B = mean(A')
return(B)
}
x1 = (1, 2, -3, 4, 5, -6)
x2 = (1 \ 2 \ 3 \ -4 \ 4)
posmean(x1)
posmean(x2)
}
end
Here you see the (simplified) program, that only works for Stata variable:
Code:
sysuse auto, clear
clear mata
mata:
real matrix function posmean(transmorphic vector x) {
real vector C, D
st_view(L=.,.,x)
C = select(L,L[.,.]:>0)
D = mean(C)
return(D)
}
posmean("price")
end
Thank you very much in advance!
Best wishes
Anika


Comment