New answers tagged

0

Here's a little one-liner without If, probably not fast on large lists: corr = (! MemberQ[elList, #] & /@ els) /. {True -> 1.13, False -> 1.0}


1

If speed is not you concern, may be this can be worth a try common = Intersection[elList, els]; u = Map[ Position[els, # ] &, common ] ; corr[[ Flatten[u] ]] = 1.13 This can be fit in a single line, and is almost readable corr[[ Flatten[ Position[els, # ] & /@ Intersection[elList, els] ] ]] = 1.13 and will seem even more nice if you use (elList ...


11

You can rewrite your idea using If as follows: If[MemberQ[elList, #], 1., 1.13] & /@ els {1.13, 1., 1., 1.13, 1.} However you may find on larger problems that repetitive use of MemberQ is not as fast as you would like, so consider a hash table in the form of an Association, or if using an older version of Mathematica a Dispatch table. Create a ...



Top 50 recent answers are included