3
$\begingroup$

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.

$\endgroup$
0

3 Answers 3

5
$\begingroup$

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."

$\endgroup$
1
  • $\begingroup$ using of Tuples is very interesting and intelligent. $\endgroup$ Commented Jan 16, 2016 at 18:12
6
$\begingroup$
MapThread[
 Append,
 {Outer[Join, a, a, 1], d},
 2
 ]
$\endgroup$
4
$\begingroup$

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

$\endgroup$
6
  • $\begingroup$ No this is not things which I have searching!! $\endgroup$ Commented Jan 16, 2016 at 13:42
  • $\begingroup$ @Ackaran. Why does neither of these methods work for you? They both produce the matrix you asked for. $\endgroup$
    – m_goldberg
    Commented Jan 16, 2016 at 14:18
  • $\begingroup$ 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. $\endgroup$ Commented Jan 16, 2016 at 14:38
  • $\begingroup$ Your answer was so helpful because it was step by step approaching. $\endgroup$ Commented Jan 16, 2016 at 18:11
  • $\begingroup$ 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. $\endgroup$ Commented Jan 16, 2016 at 18:23

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.