5
$\begingroup$

I want to use this expression for my computation:

FoldList[vt[#2[[1]], #1, #2[[2]], b] &, v0,
Transpose[Join[{ea, cm}]]] 

My problem now is that ea and cm is not only one list each it is a set of lists. How can I make a list of pairs out of these two set of lists?

For example:

ea = {{5, 6, 7}, {1, 2, 3}, {4, 8, 9}} 
cm = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}

and i want a list of pairs of these lists but again in one list each like these:

eacm = 
  {{{5, 1}, {6, 2}, {7, 3}}, {{1, 4}, {2, 5}, {3, 6}}, {{4, 7}, {8, 8}, {9, 9}}}

My real lists are longer than those shown above. I have 720 numbers in each list.

$\endgroup$
11
$\begingroup$
Transpose[{ea, cm}, {3, 1, 2}]

Flatten[{ea, cm}, {{2}, {3}, {1}}]
$\endgroup$
  • $\begingroup$ Ok but i will have the problem that i will have 10.000 lists i a row because i am doing a monte carlo simulation $\endgroup$ – user41673 Jul 16 '16 at 23:38
  • $\begingroup$ sorry you couldn´t know but thank you very much for your solution $\endgroup$ – user41673 Jul 16 '16 at 23:39
  • 7
    $\begingroup$ @user41673. I don't see how having two lists of 10000 triples invalidates this answer. $\endgroup$ – m_goldberg Jul 17 '16 at 0:10
  • $\begingroup$ A very interesting explanation about Transpose second argument here -> 16968 $\endgroup$ – Murta Jul 18 '16 at 0:51
6
$\begingroup$

This is not an answer, but an extended comment on Hotoke's answer.

The OP seems to think Transpose isn't applicable when ea and cm are very long lists, say, of 10000 triples each. Perhaps the OP thinks it would be very slow is such a case. But actually it is very fast even for long lists, as I will demonstrate here.

SeedRandom[42];
With[{n = 10000, k = 12},
  Module[{m, mt, t},
    m = RandomInteger[42, {2, n, 3}];
    t = AbsoluteTiming[mt = Transpose[m, {3, 1, 2}]][[1]];
    {t, Column[{#, mt[[#]]} & /@ RandomSample[Range[n], k]]}]]

result

It still works well even if the sublists are longer. Here is example with 5-tuples.

SeedRandom[42];
With[{n = 10000, i = 5, k = 8},
  Module[{m, mt, t},
    m = RandomInteger[42, {2, n, i}];
    t = AbsoluteTiming[mt = Transpose[m, {3, 1, 2}]][[1]];
    {t, Column[{#, mt[[#]]} & /@ RandomSample[Range[n], k]]}]]

result

$\endgroup$
  • $\begingroup$ That's some hardcore localization you got going on there ;)) $\endgroup$ – e.doroskevic Jul 17 '16 at 0:24
  • 2
    $\begingroup$ @E.Doroskevic. I like to live clean :-) $\endgroup$ – m_goldberg Jul 17 '16 at 0:29
4
$\begingroup$

Does it fit your needs?

MapThread[List, {ea, cm}, 2]
$\endgroup$
2
$\begingroup$

Example

Transpose @ # & /@ Transpose[{ea, cm}]

Note: ea and cm same as in original post

$\endgroup$
2
$\begingroup$

Transpose second argument is like magic/cryptic for me. Here is another option using Map:

Map[Transpose, Transpose@{ea, cm}, {-3}]
$\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.