# Replacing a random ith row and column from a matrix

Currently I am trying to delete a randomly-chosen $$i^{th}$$ row and column from a square $$n \times n$$ matrix $$A$$. So far I come up with the following code:

Drop[A, {RandomInteger[{1, 400}]}, {RandomInteger[{1, 400}]}]


The problem with this command is that the random integer for {i} is not the same as the random integer for {j}.

Is there a way of making them consistent, so that I drop the ith row and corresponding column while maintaining the randomness of selecting $$i$$?

And if the aim was to not delete the row and column entirely but to replace all their elements with, say 0, how would you go about it?

Thank you.

• Jan 14, 2021 at 22:55
• I edited your question title to better reflect the (final?) end goal of your question. It is better for future users to keep such things in order when you’re asking questions. Jan 15, 2021 at 1:51

Perhaps either of these:

Drop[A, {#}, {#}]&@ RandomInteger[{1, Length@A}]
With[{i = RandomInteger[{1, Length@A}]}, Drop[A, {i}, {i}]]

• Answer to the changed question: ...ReplacePart[mat, {_, i} | {i, _} -> SetPrecision[0, Precision@mat]].. Jan 14, 2021 at 22:58
A = Array[a, {7, 7}];


You can also use BlockRandom:

SeedRandom
Drop[A, {BlockRandom[RandomInteger[{1, 7}]]}, {RandomInteger[{1, 7}]}] // MatrixForm Alternatively,

SeedRandom
Drop[A, {ri = RandomInteger[{1, 7}]}, {ri}] // MatrixForm 