3
$\begingroup$

Given the following matrix:

  mat = {{{2013, 9, 10, 0, 0, 0.}, "Tuesday", 10., "September", 2013., 10., 10., 1293., 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
         {{2013, 9, 11, 0, 0, 0.}, "Wednesday", 11., "September", 2013., 11., 11., 1146., 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
         {{2013, 9, 12, 0, 0, 0.}, "Thursday", 12., "September", 2013., 12., 12., 974., 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
         {{2013, 9, 13, 0, 0, 0.}, "Friday", 13., "September", 2013., 13., 13., 1347., 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
         {{2013, 9, 16, 0, 0, 0.}, "Monday", 16., "September", 2013., 16., 16., 1207., 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
         {{2013, 9, 17, 0, 0, 0.}, "Tuesday", 17., "September", 2013., 17., 17., 1152., 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
         {{2013, 9, 18, 0, 0, 0.}, "Wednesday", 18., "September", 2013., 18., 18., 1078., 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
         {{2013, 9, 19, 0, 0, 0.}, "Thursday", 19., "September", 2013., 19., 19., 998., 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
         {{2013, 9, 20, 0, 0, 0.}, "Friday", 20., "September", 2013., 20., 20., 1220., 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
         {{2013, 9, 23, 0, 0, 0.}, "Monday", 23., "September", 2013., 23., 23., 1096., 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
        }

I also have the following matrix:

daymat = {{"Tuesday", 0., 0.0405246, 0.0475514, 0.055977, 0.0658123, 0.0620308, 0.0689194, 0.0653811, 0.074846, 0.0806566, 0.0680625, 0.065265, 0.0607537, 0.0657239, 0.0619091, 0.0518029, 0.0414976, 0.0232864, 0., 0., 0., 0., 0., 0., 0.}, 
          {"Wednesday", 0., 0.0389784, 0.0476981, 0.0553875, 0.0648306, 0.0645128, 0.0712814, 0.0666393, 0.0726954, 0.0765757, 0.0664365, 0.0629837, 0.0617724, 0.0656363, 0.0637674, 0.0558095, 0.0397895, 0.0252055, 0., 0., 0., 0., 0., 0., 0.}, 
          {"Thursday", 0., 0.0400184, 0.0478547, 0.0573118, 0.0680178, 0.0671116, 0.0719796, 0.0670419, 0.0745704, 0.0793687, 0.0660718, 0.0655258, 0.0612155, 0.0629117, 0.0615814, 0.048912, 0.0385196, 0.0219872, 0., 0., 0., 0., 0., 0., 0.}, 
          {"Friday", 0., 0.0393414, 0.047718, 0.0577941, 0.0642745, 0.060941, 0.0668365, 0.0616773, 0.0739372, 0.0744969, 0.0637601, 0.0628322, 0.0617429, 0.0604569, 0.0634777, 0.0573806, 0.049004, 0.0343285, 0., 0., 0., 0., 0., 0., 0.}, 
          {"Monday", 0., 0.0389818, 0.0466718, 0.0529945, 0.0614266, 0.0625077, 0.0673928, 0.064997, 0.0759944, 0.0822528, 0.0678311, 0.0643834, 0.0607663, 0.064325, 0.0655287, 0.0557701, 0.0422659, 0.02591, 0., 0., 0., 0., 0., 0., 0.}
         }

I want to join daymat to mat under the condition that day of week in daymat has to match with day of week in mat. For example, the 1st row of daymat, which is "Tuesday" will join with the 1st row in mat, which has the same day of week ("Tuesday").

How can I do that ?

Thanks

$\endgroup$
0

4 Answers 4

2
$\begingroup$

Map[Function[arg, Append[arg, Select[daymat, #[[1]] == arg[[2]] &]]], mat] will "join" them, tinker as needed to get format you want.

$\endgroup$
4
$\begingroup$
Replace[mat, {x_, z : #, y__} :> {x, z, y, ##2} & @@@ daymat, 1] 
{{{2013,9,10,0,0,0.},Tuesday,10.,September,2013.,10.,10.,1293.,0,0, <<39>>, 0.0414976, 
  0.0232864,0.,0.,0.,0.,0.,0.,0.},<<8>>,{<<1>>}}
$\endgroup$
1
$\begingroup$

Is this what you mean?

Map[FlattenAt[Append[#, Cases[daymat, {#[[2]], n___} -> n, {1}, 1]], -1] &, mat]
$\endgroup$
1
$\begingroup$

Create an association:

daypos = Association@MapIndexed[First@#1 -> First@#2 &, daymat]

<|"Tuesday" -> 1, "Wednesday" -> 2, "Thursday" -> 3, "Friday" -> 4,
"Monday" -> 5|>

Join[#
   , Rest@daymat[[   daypos[#[[2]]] ]]
   ] & /@ mat

Explanation

Join every row of mat with the rest of the row from daymat whose index is looked up using the association.

Alternate syntax with fewer brackets:

Join[#
   , Rest@Part[daymat
     , daypos[First@Rest@#]]
   ] & /@ mat
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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