3
$\begingroup$

I think the title says it all. I am looking for a function f[A_matrix, r_Integer, c_Integer] that will return the matrix A with the row r and column c deleted. I know how to delete rows but how about columns?

Thanks, I appreciate

$\endgroup$
2

2 Answers 2

6
$\begingroup$

You are after Drop

enter image description here

Data example

MatrixForm[
    matrx=Outer[Row@*List, CharacterRange["A", "E"], Range[5]]
]

enter image description here

Now you can do Drop[matrix, {row},{column}]

MatrixForm[
     Drop[matrx, {3},{2}]
]

enter image description here

If you want to define your function

f[a_List?MatrixQ, row_Integer, column_Integer] := Drop[a, {row},{column}]
$\endgroup$
5
$\begingroup$
(mat = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}) // MatrixForm

Mathematica graphics

del[mat_?MatrixQ, col_Integer?(Positive), row_Integer?(Positive)] := 
 Module[{m = mat},
  m[[All, col]] = Sequence[];
  m[[row]] = Sequence[];
  m
  ];

Now

del[mat, 1, 1] // MatrixForm

Mathematica graphics

del[mat, 2, 2] // MatrixForm

Mathematica graphics

$\endgroup$
3
  • 1
    $\begingroup$ You can also assign to Nothing instead of Sequence[] $\endgroup$ Commented Feb 24, 2023 at 15:38
  • $\begingroup$ @GustavoDelfino, yes, that works also. $\endgroup$
    – Nasser
    Commented Feb 24, 2023 at 15:39
  • $\begingroup$ Great guys, I didn't know about this "All" thing, thank you! $\endgroup$
    – Georgy
    Commented Feb 24, 2023 at 15:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.