Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have two lists a and b of the same length. And another list a2 which contains all elements from a and some others. Now I want to construct a list b2 so that for the shared elements between a2 and a, the corresponding elements in b2 and b are the same, and for those elements in a2 but not a, the values in b2 should be placeholder 0.

Example:

a = {1, 2, 5, 8};
b = {10, 20, 50, 80};
a2 = {1, 2, 4, 5, 8, 9};
(*Output b2={10,20,0,50,80,0}*)

I know how to achieve this in C-like languages, so I'm wondering if there is a better way of doing this in Mathematica. Thanks!

P.S. a and a2 are both ordered. And no duplicate exsists in either list, if it helps.

share|improve this question
up vote 5 down vote accepted

For all versions:

Replace[a2, Append[Thread[a -> b], _ -> 0], 1]
{10, 20, 0, 50, 80, 0}

If you need speed look at Dispatch.

For versions 10.0 or later Association functionality is fast and concise:

Lookup[AssociationThread[a, b], a2, 0]
{10, 20, 0, 50, 80, 0}

Tersely Thread works in place of AssociationThread but it causes a redundant evaluation:

Lookup[Thread[a -> b], a2, 0]
{10, 20, 0, 50, 80, 0}
share|improve this answer
2  
Waw, nice to see you back and to see such nice answer. Man when are finishing your book! – Algohi Jan 14 at 21:35
Fold[Insert[#1, 0, #2] &, b, Position[Map[Position[a, #] &, a2], {}]]

{10, 20, 0, 50, 80, 0}

Probably faster with large lists:

Fold[Insert[#1, 0, #2] &, b, Position[a2, Alternatives @@ Complement[a2, a]]]
share|improve this answer

The most obvious :

If[MemberQ[a, #], b[[Position[a, #][[1, 1]]]], 0] & /@ a2

{10, 20, 0, 50, 80, 0}

share|improve this answer

Some pure "mathematical" ad hoc:

a2 /. Thread[a -> b] /. Thread[Complement[a2, a] -> 0]

{10, 20, 0, 50, 80, 0}

share|improve this answer
mia = MapIndexed[Rule[#2, t[#1]] &, a];
mib = MapIndexed[Rule[#2, #1] &, b];
mic = MapIndexed[Rule[t[#1], #2] &, a2];
Normal@SparseArray[mib /. (mia /. mic), Length[a2]]
(*{10, 20, 0, 50, 80, 0}*)
share|improve this answer
 b2 = Sort[Join[{#, 0} & /@ Complement[a2, a], Join[Partition[Riffle[a, b], 2]]]][[All, 2]]

{10, 20, 0, 50, 80, 0}

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.