4
$\begingroup$

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?

$\endgroup$

4 Answers 4

6
$\begingroup$

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}]];

Mathematica graphics

This gives same result as the other answer

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

Mathematica graphics

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

Mathematica graphics

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

$\endgroup$
3
  • 1
    $\begingroup$ Lisp was only like 5 years after Fortran $\endgroup$
    – lericr
    May 5, 2023 at 20:19
  • $\begingroup$ Oh no, the image plague has hit this site as well. $\endgroup$ May 6, 2023 at 13:09
  • 1
    $\begingroup$ @PeterMortensen you downvoted this answer because it has images of the output? $\endgroup$
    – Nasser
    May 6, 2023 at 19:15
7
$\begingroup$
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}]
$\endgroup$
3
$\begingroup$

(1)

MapThread[Sequence@@@Insert[##,{5}]&, {Drop[Tab1,None,{5,6}],Tab2}]==Tab3

 (* 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 *) 
$\endgroup$
1
$\begingroup$
a = RandomReal[{0, 1}, {10, 8}];
b = RandomReal[{1, 2}, {10, 4}];

c = Join[a[[All, {1, 2, 3, 4}]], b, a[[All, {7, 8}]], 2];

Using ReplaceAt (new in 13.1)

d =
  Module[{i = 1},
   ReplaceAll[x :> Splice @ b[[i++]]] @
    ReplaceAt[_ :> x, {All, 5}] @
     ReplaceAt[a, _ :> Nothing, {All, 6}]];

d == c

True

Using TakeList

join[a_, b_] :=
 Module[{x = TakeList[#, {4, 2, 2}] & /@ a},
  x[[All, 2]] = b;
  Join @@@ x]

join[a, b] == c

True

$\endgroup$
1
  • $\begingroup$ (+1) Very nice, @eldo! :) $\endgroup$ Apr 1 at 1:16

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.