Group Abstract Group Abstract

Message Boards Message Boards

Insert or Joint a element in a table

Hi, I have a this data table

a = { { {a,b},{c,d},{e,f} } , { {1,2},{3,4},{5,6} } }

and

t = {x , z }

I would obtain this

b = { { {a,b,x},{c,d,x},{e,f,x} } , { {1,2,z},{3,4,z},{5,6,z} } }

I have tried with Insert and joint but unsucesfully. Any suggestions please? Thank you

Margherita

7 Replies

Flatten always scares me with regards to scalability, thus I prefer

Append @@@ # & /@ Thread /@ Transpose[{aa, t}]
POSTED BY: David Gathercole

Thank you a lot !

Posted 11 years ago

Here probably the same question: How to insert elements of a list into list of lists?

MapThread[Flatten /@ Thread[{##}] &, {aa, t}]
{{{a, b, x}, {c, d, x}, {e, f, x}}, {{1, 2, z}, {3, 4, z}, {5, 6, z}}}
POSTED BY: Kuba Podkalicki

Ooops, that is a tricky one!

aa = {{{a, b}, {c, d}, {e, f}}, {{1, 2}, {3, 4}, {5, 6}}};
t = {x, z};

Flatten /@ # & /@ Thread /@ Thread[{aa, t}]

... well, I prefer Davids solution!

Henrik

POSTED BY: Henrik Schachner
Posted 11 years ago

I renamed the a and b on the left side to avoid recursion and used rules to perform the transformations. Look up Rule, and Replace for more insight.

Kind regards, David

In[1]:= aa = {{{a, b}, {c, d}, {e, f}}, {{1, 2}, {3, 4}, {5, 6}}};

In[2]:= t = {x, z};

In[3]:= bb = {
  aa[[1]] /. {e1_, e2_} -> {e1, e2, t[[1]]},
  aa[[2]] /. {e1_, e2_} -> {e1, e2, t[[2]]}
  }

Out[3]= {{{a, b, x}, {c, d, x}, {e, f, x}}, {{1, 2, z}, {3, 4, z}, {5, 6, z}}}
POSTED BY: David Keith
Posted 11 years ago

delete

POSTED BY: Bill Simpson
Posted 11 years ago
POSTED BY: Bill Simpson
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard