1

Let's say I have a 3x3 matrix and I want to insert zero vector into all second row and all second column of the matrix. At the end, the previous second row shifted to third row and the previous second column shifted to third column. The new second row and column terms will all be zero. How could I solve this problem ?

rhermans
  • 36,518
  • 4
  • 57
  • 149
user58886
  • 11
  • 1
  • Welcome! To make the most of Mma.SE start by taking the [tour] now. It will help us to help you if you write an excellent question. Edit if improvable, show due diligence, give brief context, include minimal working example of code and data you have tried yourself. Use formatted form. As you receive give back, vote and answer questions, keep the site useful, be kind, correct mistakes and share what you have learned. Why not choosing a meaningful name? – rhermans Jul 12 '18 at 17:05
  • Your question may be put on-hold because it may be considered a duplicate of this question and therefore off-topic. Please [edit] your question if you consider this is a mistake and give great emphasis in what was NOT answered in the other question. Please don't be discouraged by that cleaning-up policy. Your questions are and will be most welcomed. Learn about good questions here. – rhermans Jul 12 '18 at 17:07

1 Answers1

2
(mat = Partition[Range[25], 5]) // MatrixForm

$\left( \begin{array}{ccccc} 1 & 2 & 3 & 4 & 5 \\ 6 & 7 & 8 & 9 & 10 \\ 11 & 12 & 13 & 14 & 15 \\ 16 & 17 & 18 & 19 & 20 \\ 21 & 22 & 23 & 24 & 25 \\ \end{array} \right)$

mat2 = ArrayFlatten[ReplacePart[TakeList[mat,
                                         {1, 0, UpTo[∞]}, {1, 0, UpTo[∞]}],
                                {{1, 2}, {2, 1}, {2, 2}, {2, 3}, {3, 2}} -> 0]]
MatrixForm[mat2]

$\left( \begin{array}{cccccc} 1 & 0 & 2 & 3 & 4 & 5 \\ 0 & 0 & 0 & 0 & 0 & 0 \\ 6 & 0 & 7 & 8 & 9 & 10 \\ 11 & 0 & 12 & 13 & 14 & 15 \\ 16 & 0 & 17 & 18 & 19 & 20 \\ 21 & 0 & 22 & 23 & 24 & 25 \\ \end{array} \right)$

Coolwater
  • 20,257
  • 3
  • 35
  • 64
  • Thank you for your answer @Coolwater but my mathematica v.9.0.1.0 and does not recognize TakeList, unfortunately. – user58886 Jul 12 '18 at 20:27
  • maybe I could use Take instead of TakeList, however I could not understand what it means {1,0,UpTo[∞]} – user58886 Jul 12 '18 at 20:31
  • 2
    {row, col} = {3, 4}; mat = Insert[mat, 0 mat[[row]], row]; mat = Transpose@Insert[Transpose@mat, 0 mat[[All, col]], col]; mat // MatrixForm – OkkesDulgerci Jul 12 '18 at 20:57