0

Consider the following table:

tabb = Table[RandomReal[{0, 100}, 100], {i, 1, 10, 1}];

How to replace its kth column (say, the 52th column) by the column

col=Table[{i},{i,1,10,1}]?

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
John Taylor
  • 5,701
  • 2
  • 12
  • 33
  • 4
    tabb[[All, 52]] = col. Since Mathematica doesn't differentiate between column and row vectors, you might just want to have a plain list there. – Syed Jun 28 '22 at 15:13

2 Answers2

4

I assume your data is just made up for an example, but just for reference, you can do this:

tabb = RandomReal[{0, 100}, {10, 100}]

I also assume that you don't intend to replace numbers with lists, so let's simplify your new column:

col = Range@10

Now we have a couple of options. If you want to mutate in place, you can use Set directly:

tabb2[[All, 52]] = col

If you want a new structure, one option is SubsetMap:

newtabb = SubsetMap[col &, tabb, {All, 52}]

Another is MapThread:

newtabb = MapThread[ReplacePart[#1, 52 -> #2] &, {tabb, col}]

Another is just ReplacePart with patterns:

newtabb = ReplacePart[tabb, {row_, 52} :> col[[row]]]
lericr
  • 27,668
  • 1
  • 18
  • 64
3

To clearly see the results it is better to use symbolic arrays and vectors. And dimensions on the order of 100 are excessive in a minimal example.

Clear["Global`*"]

(Format[#[i__]] := Subscript[#, Row[{i}]]) & /@ {a, b};

(tabb = Array[b, {5, 8}]) // MatrixForm

enter image description here

col = Array[Style[a[#], Red] &, Length@tabb];

As Syed indicated, to replace column 3

tabb[[All, 3]] = col;

tabb // MatrixForm

enter image description here

Bob Hanlon
  • 157,611
  • 7
  • 77
  • 198