Use the colon (:
) operator for element-by-element calculations.
mata
V = (1 \ 2 \ 3 \ 4)
V
V :== 1
end
See help [M-2] op_colon
for details.
Question
Suppose I have a vector foo
defined in Mata thus:
mata: foo = (1,2,1,3,1,4)
How do I perform an element-wise test on foo
? For example, in R, if I define foo thus:
foo = c(1,2,1,3,1,4)
I can simply type:
foo == 1
and I obtain as a result a vector of boolean values for the element-wise test == 1
:
TRUE FALSE TRUE FALSE TRUE FALSE
which I can then assign or perform operations on (e.g. count up the number of TRUE
or FALSE
values).
I could, of course take several lines of code to loop over each element, but wonder if the language has an elegant syntax for this task already.
Solution
Use the colon (:
) operator for element-by-element calculations.
mata
V = (1 \ 2 \ 3 \ 4)
V
V :== 1
end
See help [M-2] op_colon
for details.