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

This question is an extension of the 43122. I’ve looked through many interesting post (like this) but (as always) missed the direct matching. So the setting is simple:

datum = {{{2, g},         {6, h}, {7, k}}, 
         {{2, a}, {4, b},         {7, d}}, 
                 {{4, e},         {7, f}}}

Note: numbers {2, 4, 6, 7} are ordered but arbitrary (that differs with related question that seems promising). I need reliable and possibly concise method suitable for working with matrices of the size around 1000 x 1000 (that are as a rule time series). May be some specific ad hoc build-in methods.

Expected result ("X" any constant, i.e. Missing[]):

{
 {{2, g},   {4, "X"}, {6, h},   {7, k}},
 {{2, a},   {4, b},   {6, "X"}, {7, d}},
 {{2, "X"}, {4, e},   {6, "X"}, {7, f}}
}

PS. My current method seems, sort of, naive and long.

sample = Union @@ (#[[All, 1]] & /@ datum)
(* {2, 4, 6, 7} *)
step1 = Complement[sample, #[[All, 1]]] & /@ datum;
step2 = Thread[{#, Array["X" &, Length[#]]}] & /@ step1;
MapThread[Union, {datum, step2}]

Thanks in advance to everyone who respond.

share|improve this question
up vote 3 down vote accepted
Apply[List,
  Normal@*KeySort /@ KeyUnion[
    AssociationThread @@@ Transpose /@ datum
    , "X" &
  ]
  , {2}]

Explanation

AssociationThread @@@ Transpose /@ datum generates a list of Associations with the first elements (2, 4, 6, and 7) as the Keys. KeyUnion then takes each of those Associations and adds the Keys from the other Associations that are missing; the second argument specifies what should be done in the situation where a Key is Missing. KeySort does exactly what it seems to, Normal changes the Association to a list of Rules, and finally, we change each of the Rules to a List using Apply.

Old version:

assocs = Association /@ Apply[Rule, datum, {2}];
assocsUnion = KeySort /@ KeyUnion@assocs /. Missing[__] :> "X";
Apply[List, Normal@assocsUnion, {2}]
share|improve this answer
datum = {{{2, g}, {6, h}, {7, k}}, {{2, a}, {4, b}, {7, d}}, {{4, e}, {7, f}}};

With[{uni = Union@Cases[datum, a_Integer :> {a, "X"}, 3]},
  Sort /@ DeleteDuplicatesBy[First] /@ (Join[#, uni] & /@ datum)] // MatrixForm

enter image description here

share|improve this answer

This is an entirely pre V10 method:

With[{xtra = Thread[{Union@Flatten@datum[[All, All, 1]], Missing[]}]},
  Sort@DeleteDuplicates[Join[#, xtra], #1[[1]] == #2[[1]] &] & /@ datum
 ]

which gives:

(*

{{{2, g}, {4, Missing[]}, {6, h}, {7, k}}, {{2, a}, {4, b}, {6, 
   Missing[]}, {7, d}}, {{2, Missing[]}, {4, e}, {6, Missing[]}, {7, 
   f}}}

*)
share|improve this answer

With set functions and a pure listable function

With[{indices = Union @@ datum[[All, All, 1]]},
 Union[Function[{v}, {v, Missing[]}, Listable][
     Complement[indices, #[[All, 1]]]], #] & /@ datum]

{{{2, g}, {4, Missing[]}, {6, h}, {7, k}}, {{2, a}, {4, b}, {6, Missing[]}, {7, d}}, {{2, Missing[]}, {4, e}, {6, Missing[]}, {7, f}}}

Hope this helps.

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.