While there are plenty of decent answers in the comments, I wanted to provide a couple pieces of information about why the solutions are generally complicated and why this question is odd for Mathematica.
Primarily, something that is true for Mathematica but that is generally hidden from you as a user is that the lists and data in Mathematica are usually immutable, meaning that you do not really 'copy' items from a
to b
; instead, you create a new list that has some elements of a
and some elements of b
. In languages like Matlab, you can do something along the lines of b[a>2] = a[a>2]
, in which the elements of b
get overwritten in place. This is not possible in Mathematica because the underlying list is immutable. If you are used to a language like Matlab, this may seem unintuitive, but it allows for several kinds of optimization (data never needs to be copied, multiple threads can be employed without worries about race conditions, etc.).
Instead of copying elements around, the usual way to approach this problem is to operate over the data simultaneously, building up a new representation; here are a few examples:
b = If[#1 > 2, #1, #2] & @@@ Transpose[{a, b}]
{0,0,0,3,4}
b = Function[{aa, bb}, If[aa > 2, aa, bb], {Listable}][a, b]
{0,0,0,3,4}
Note that the statement b = ...
looks like mutability (and in a sense, it is); symbol bindings are mutable, as is basically everything in Mathematica's underlying pattern matching machinery. In other words, you can rebind b
to a new data value, but you cannot change the data that b
points to.
b = a UnitStep[a - 3]
? $\endgroup$b
is all zero to begin with and what if it is not? $\endgroup$