1
$\begingroup$

Consider two tables

Tab1 = Table[{{RandomReal[{0, 1}], 
     RandomReal[{0, 1}]}, {RandomReal[{0, 1}], 
     RandomReal[{0, 1}]}, {RandomReal[{0, 1}], 
     RandomReal[{0, 1}]}}, {i, 1, 5, 1}];
Tab2 = Table[{{RandomReal[{0, 1}], 
     RandomReal[{0, 1}]}, {RandomReal[{0, 1}], 
     RandomReal[{0, 1}]}}, {i, 1, 5, 1}];

Their rows are also tables:

Tab2//TableForm

enter image description here

I would like to make several manipulations with the tables:

  1. Combine the tables in a way such that their sub-tables will be merged. The ugly realization (in the sense that it may be slow if Tab1 has a large size) is

    TabJoined=Table[Join[Tab1[[i]],Tab2[[i]]],{i,1,Length[Tab1],1}];

  2. Extract only the first row from each sub-table of Tab1. The ugly realization is

    TabRowExtracted=Table[Tab1[[i]][[2]],{i,1,Length[Tab1],1}];

  3. Multiply each 2nh element of the second row of Tab1 by 2:

    TabSecondElementMultiplied=Table[{Tab1[[i]][1],{Tab1[[i]][[2]][[2]],2*Tab1[[i]][[2]][[2]]},Tab1[[i]][[3]]},{i,1,Length[Tab1],1}];

Could you please tell me whether there are faster realizations of these tasks?

$\endgroup$
2
  • 3
    $\begingroup$ One can also use Tab1=RandomReal[{0,1},{5,3,2}]; Tab2=RandomReal[{0,1},{5,2,2}];. $\endgroup$
    – user293787
    Oct 26, 2022 at 9:19
  • 1
    $\begingroup$ @user293787 : thanks! $\endgroup$ Oct 26, 2022 at 9:31

2 Answers 2

3
$\begingroup$
Join[Tab1,Tab2,2]
Tab1[[;;,2]]
MapAt[2*#&,Tab1,{All,2,2}]
$\endgroup$
2
$\begingroup$

That's what I understand from the description in the post. Please provide feedback, if I have not understood it correctly.

SeedRandom[1];
Tab1 = RandomReal[{0, 1}, {5, 3, 2}];
Tab2 = RandomReal[{0, 1}, {5, 2, 2}];

enter image description here

Combine the tables in a way such that their sub-tables will be merged

(TabJoined = 
   Transpose@Catenate[{Transpose@Tab1, Transpose@Tab2}]) // MatrixForm

enter image description here


Extract only the first row from each sub-table of Tab1

MatrixForm /@ {Tab1, Tab1[[All, All, 1]]}

enter image description here


Multiply each 2nd element of the second row of Tab1 by 2

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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