Efficient method for Inserting arrays into arrays
Q1
Join[a[[;; , ;; 1]], b, a[[;; , 2 ;;]], 2]
One may want to create function
insCol[a_, b_, n_] := Join[a[[;; , ;; n - 1]], b, a[[;; , n ;;]], 2]
insRow[a_, b_, n_] := Join[a[[;; n - 1]], b, a[[n ;;]]]
After a few attempts, the following gives a noticeable if unremarkable improvement over insertarray
:
columnInsert[a_, b_, pos_] :=
MapThread[Join[#1[[;; pos - 1]], #2, #1[[pos ;;]]] &, {a, b}];
Your second functionality doensn't actually need anything fancy, just some swaps afterwards:
SetAttributes[columnInsert`swap, HoldFirst];
columnInsert`swap[mat_, first_, list_] :=
Module[{i = first},
Scan[columnInsert`tmp = mat[[All, i]];
mat[[All, i++]] = mat[[All, #]];
mat[[All, #]] = columnInsert`tmp; &, list]; mat]
columnInsert[a_, b_, pos_List] :=
Module[{res = columnInsert[a, b, First@pos]},
columnInsert`swap[res, First@pos, pos]; res]