2
$\begingroup$

I have 3 lists called as List6, List7 and List8 which are as:

List6 = Table[{x1[[i, j]]}, {i, 101}, {j, 101}];
List7 = Table[{y1[[i, j]]}, {i, 101}, {j, 101}];
List8 = Table[{xDisp1[[i, j]]}, {i, 101}, {j, 101}];

I want to combine them and use a code below

data = MapThread[{#1[[1]], #2[[1]], #3[[1]]} &, {List6, List7, 
List8}];

to achieve a list which will have terms such that

{{x1[[1,1]],y1[[1,1]],xDisp1[[1,1]]},{x1[[1,2]],y1[[1,2]],xDisp[[1,2]]},...,
 {x1[[101,101]],y1[[101,101]],xDisp[[101,101]]}}

but I cannot achieve with that code. Where do I make a mistake and how is it possible to correct the code, please ?

Best Regards

$\endgroup$
3
$\begingroup$

You may use Transpose.

As you have not provided the definitions for x1, y1, and xDisp1 I will use indices in the values place. I will also use a shorter series for the demonstration.

With

list6 = Table[{x1[i, j]}, {i, 2}, {j, 2}];
list7 = Table[{y1[i, j]}, {i, 2}, {j, 2}];
list8 = Table[{xDisp1[i, j]}, {i, 2}, {j, 2}];

Then

Flatten[Transpose[{list6, list7, list8}, {4, 2, 3, 1}], 2]
{{x1[1, 1], y1[1, 1], xDisp1[1, 1]}, {x1[1, 2], y1[1, 2], xDisp1[1, 2]}, 
 {x1[2, 1], y1[2, 1], xDisp1[2, 1]}, {x1[2, 2], y1[2, 2], xDisp1[2, 2]}}

Key to understanding the above is to examine the Dimensions of the list you want to Transpose.

Dimensions@{list6, list7, list8}
{3, 2, 2, 1}

Next notice that you want the 4th-dim (the individual entries), then the 2nd- and 3rd-dim (the row and columns), and finally 1st-dim (the items from each set). Flatten is used to reduce dimensions of the result by flattening out 2 dimensions (row and column).

Note that starting in version 11.2 you have an shorthand method with TwoWayRule since you are doing one level swap.

Flatten[Transpose[{List6, List7, List8}, 1 <-> 4], 2]

Hope this helps.

$\endgroup$
1
$\begingroup$

The simplest way is just to Flatten your lists first, and then use Transpose. Following @Edmund's use of indices rather than lists for x1, y1 and xDisp, take

List6 = Table[{x1[i, j]}, {i, 101}, {j, 101}];
List7 = Table[{y1[i, j]}, {i, 101}, {j, 101}];
List8 = Table[{xDisp1[i, j]}, {i, 101}, {j, 101}];

Then

data = Transpose[Flatten /@ {List6, List7, List8}];

gives you your output. The dimensions of data and its first and last three entries are:

Dimensions[data]
data[[1 ;; 3]]
data[[-3 ;; -1]]

(* {10201, 3}

   {{x1[1, 1], y1[1, 1], xDisp1[1, 1]}, {x1[1, 2], y1[1, 2], xDisp1[1, 2]}, 
    {x1[1, 3], y1[1, 3], xDisp1[1, 3]}}

   {{x1[101, 99], y1[101, 99], xDisp1[101, 99]}, {x1[101, 100], y1[101, 100], xDisp1[101, 100]}, 
    {x1[101, 101], y1[101, 101], xDisp1[101, 101]}} *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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