# How can I join these two tables in the following way?

Consider these two tables:

Tab1 = RandomReal[{0, 1}, {10, 8}];
Tab2 = RandomReal[{1, 2}, {10, 4}];

I would like to replace the 5th and 6th columns of Tab1 with Tab2, i.e.,

Tab3 = Join[Tab1[[All, {1, 2, 3, 4}]], Tab2, Tab1[[All, {7, 8}]], 2];

Is there another way of doing this?

Is there any other way of doing this?

Since you asked for other ways, this is how we did these things in the good old days of Fortran programming before all this functional programming craze hit the world :)

tab1 = RandomReal[{0, 1}, {10, 8}];
tab2 = RandomReal[{1, 2}, {10, 4}];

And now

new = Table[0, {10}, {10}];
new[[All, Range[4]]] = tab1[[All, 1 ;; 4]];
new[[All, Range[5, 8]]] = tab2[[All, ;;]];
new[[All, Range[9, 10]]] = tab1[[All, {7, 8}]];

This gives same result as the other answer

MapThread[ReplacePart[#1, {5 -> Splice[#2], 6 -> Nothing}] &, {tab1, tab2}] // MatrixForm

Which is also same answer as your method (which I think is easier than my Fortran style to understand) and I see nothing wrong with it.

Join[tab1[[All, {1, 2, 3, 4}]], tab2, tab1[[All, {7, 8}]], 2] // MatrixForm

The most important thing is not how short or long the code is, but how simple and easy to understand it is.

• Lisp was only like 5 years after Fortran May 5, 2023 at 20:19
• Oh no, the image plague has hit this site as well. May 6, 2023 at 13:09
• @PeterMortensen you downvoted this answer because it has images of the output? May 6, 2023 at 19:15
MapThread[ReplacePart[#1, {5 -> Splice[#2], 6 -> Nothing}] &, {Tab1, Tab2}]

or

MapThread[Insert[#1, Splice[#2], 5] &, {Drop[Tab1, {}, {5, 6}], Tab2}]

or

MapThread[Riffle[#1, #2, {5, 8, 1}] &, {Drop[Tab1, {}, {5, 6}], Tab2}]

(1)

(* True *)

(2)

Join[Drop[Tab1,None,{5,6}],Tab2,2][[All,Permute[Range[10], Cycles[{{5,9,7},{6,10,8}}]]]]==Tab3

(* True *)

(3)

Join[Tab1,Tab2,2][[All,Permute[Range[12],
Cycles[{{5,11,7,9},{6,12,8,10}}]]]][[All,;;10]]==Tab3

(* True *)