Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

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 a matrix and two lists:

matrix = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}};
a = {{1, 2, 2, 1}, {3, 4, 4, 3}, {8, 5, 5, 8}}; 
d = {{I, 2, -I}, {I, 1, -1}, {4, I, 0}};

I am trying to do write a code as psudo-code here to joined a and b and add them to matrix:

Do[matrix[[i, j]] = AppendTo[a[[i]], a[[j]]], {i, 0, 3}, {j, 0, 3}];

the desired result is as below

matrix = 
  {{{1, 2, 2, 1, 1, 2, 2, 1, d[[1, 1]]}, 
    {1, 2, 2, 1, 3, 4, 4, 3, d[[1, 2]]}, 
    {1, 2, 2, 1, 8, 5, 5, 8, d[[1, 3]]}}, 
   {{3, 4, 4, 3, 1, 2, 2, 1, d[[2, 1]]}, 
    {3, 4, 4, 3, 3, 4, 4, 3, d[[2, 2]]}, 
    {3, 4, 4, 3, 8, 5, 5, 8, d[[2, 3]]}}, 
   {{8, 5, 5, 8, 1, 2, 2, 1, d[[3, 1]]}, 
    {1, 2, 2, 1, 3, 4, 4, 3, d[[3, 2]]}, 
    {8, 5, 5, 8, 8, 5, 5, 8, d[[3, 3]]}}};

I would be so glad to have a way to obtain the result. Also, it will be good if I have a final matrix in horizontal shape for its sub_matrices in the nested final matrix instead of column shape for them.

share|improve this question
up vote 5 down vote accepted

Xavier's comment with an additional Partition gives:

Partition[Flatten /@ Transpose[{Tuples[a, {2}], Flatten@d}], Length@d] 

enter image description here

I think this is what the OP had in mind but it doesn't correspond to the next to last row of his "desired result."

share|improve this answer
    
using of Tuples is very interesting and intelligent. – Irreversible Jan 16 at 18:12
MapThread[
 Append,
 {Outer[Join, a, a, 1], d},
 2
 ]
share|improve this answer

Let's build your matrix in three steps.

a = {{1, 2, 2, 1}, {3, 4, 4, 3}, {8, 5, 5, 8}};
d = {{I, 2, -I}, {I, 1, -1}, {4, I, 0}};

m1 = Join[#, #] & /@ a
{{1, 2, 2, 1, 1, 2, 2, 1}, 
 {3, 4, 4, 3, 3, 4, 4, 3}, 
 {8, 5, 5, 8, 8, 5, 5, 8}}
m2 = ConstantArray[#, 3] & /@ m1
{{{1, 2, 2, 1, 1, 2, 2, 1}, 
  {1, 2, 2, 1, 1, 2, 2, 1}, 
  {1, 2, 2, 1, 1, 2, 2, 1}}, 
 {{3, 4, 4, 3, 3, 4, 4, 3}, 
  {3, 4, 4, 3, 3, 4, 4, 3}, 
  {3, 4, 4, 3, 3, 4, 4, 3}}, 
 {{8, 5, 5, 8, 8, 5, 5, 8}, 
  {8, 5, 5, 8, 8, 5, 5, 8}, 
  {8, 5, 5, 8, 8, 5, 5, 8}}}
matrix = MapThread[Append, {m2, d}, 2]
{{{1, 2, 2, 1, 1, 2, 2, 1, I}, 
  {1, 2, 2, 1, 1, 2, 2, 1, 2}, 
  {1, 2, 2, 1, 1, 2, 2, 1, -I}}, 
 {{3, 4, 4, 3, 3, 4, 4, 3, I}, 
  {3, 4, 4, 3, 3, 4, 4, 3, 1}, 
  {3, 4, 4, 3, 3, 4, 4, 3, -1}}, 
 {{8, 5, 5, 8, 8, 5, 5, 8, 4}, 
  {8, 5, 5, 8, 8, 5, 5, 8, I}, 
  {8, 5, 5, 8, 8, 5, 5, 8, 0}}}

This, of course, can be nested into one expression.

matrix = MapThread[Append, {(ConstantArray[#, 3] & /@ (Join[#, #] & /@ a)), d}, 2]

But it is much harder to understand how it works in that form.

Update

Another approach to solving this problem is to break it into two parts -- to first write a helper function that can generate any single row of the desired matrix and then map that function over the two given matrices to produce the desired matrix.

helper[aRow_, dRow_] := 
  MapThread[Append, {ConstantArray[Join[aRow, aRow], 3], dRow}]

matrix = MapThread[helper, {a, d}]

Update 2

matrix // MatrixForm

matrix

share|improve this answer
    
No this is not things which I have searching!! – Irreversible Jan 16 at 13:42
    
@Ackaran. Why does neither of these methods work for you? They both produce the matrix you asked for. – m_goldberg Jan 16 at 14:18
    
So sorry but I just saw the line above "This of course can be", I could not see the continued writing. it is correct. Thanks a bunch. – Irreversible Jan 16 at 14:38
    
Your answer was so helpful because it was step by step approaching. – Irreversible Jan 16 at 18:11
    
But I had thought this is correct but your answer after checking is not which I am searching. My figure is removed on my post I will posted again. output must be similar to that. I think eldo write the correct one. – Irreversible Jan 16 at 18:23

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.