5
$\begingroup$

How do I drop the first/last row or first/last column from a matrix? The general case is answered here but I am new to Mathematica and it the arbitrary case is unnecessarily complex.

Suppose I am dealing with a 4 by 4 square matrix such as

m = Array[Subscript[a, ##] &, {4, 4}]

and would like to delete the first and last rows as well as columns.

This may involve a simple application of the drop function I reckon. I would appreciate a very barebones example.

$\endgroup$
6
  • 2
    $\begingroup$ There are some nice tutorials about linear algebra in Mma. reference.wolfram.com/language/guide/… Look under "Learning Resources". Delete[m, 1] removes the first row from a matrix m. Delete[m, -1] removes the final row. Transpose the matrix to do the same things to columns. $\endgroup$
    – bill s
    Feb 28, 2015 at 19:13
  • 1
    $\begingroup$ I have marked this question as a duplicate. (See the link inserted at the top of your question.) Please review it, and if you feel your question is not answered there edit this question to explain why. $\endgroup$
    – Mr.Wizard
    Feb 28, 2015 at 19:32
  • 1
    $\begingroup$ Bare bones example. Given m = Array[a[#1, #2] &, {4, 4}] , dropping the first row, first column, last row, and last column from m can done with Drop[m, {1, 4, 3}, {1, 4, 3}] giving {{a[2, 2], a[2, 3]}, {a[3, 2], a[3, 3]}}. $\endgroup$
    – m_goldberg
    Mar 1, 2015 at 0:19
  • $\begingroup$ @Mr.Wizard thanks so much for looking after these questions. I have edited this question slightly to ensure it is no longer a duplicate but asking for a barebones example instead. $\endgroup$
    – Hirek
    Mar 1, 2015 at 9:40
  • 2
    $\begingroup$ If you specifically want to know how to drop first / last rows and columns, the general case linked by @Mr.Wizard is heavy going. I suggest Most@m Most /@ m Rest@m Rest /@ m (I was surprised when Wolfram added Most and Rest to the language, because the general case was already available, but I find them very useful and much more readable.) $\endgroup$
    – djp
    Mar 1, 2015 at 22:05

3 Answers 3

10
$\begingroup$

Okay, following the "bare bones" approach for dropping rows and columns here are a few ideas.

Starting matrix:

m = Array[Times, {5, 6}]

$\left( \begin{array}{ccccc} 1 & 2 & 3 & 4 & 5 \\ 2 & 4 & 6 & 8 & 10 \\ 3 & 6 & 9 & 12 & 15 \\ 4 & 8 & 12 & 16 & 20 \\ \end{array} \right)$

Part

When possible I use Part:

  1. Highly efficient
  2. Can select (or drop) rows and columns at the same time
  3. Common syntax that will be useful for other operations

To "delete the last row and first column" we can use:

m[[;; -2, 2 ;;]]

$\left( \begin{array}{cccc} 2 & 3 & 4 & 5 \\ 4 & 6 & 8 & 10 \\ 6 & 9 & 12 & 15 \\ \end{array} \right)$

This uses Span. The specification for Part is in the order of rows, then columns, as this is how Mathematica arrays are composed. The first specification is:

;; -2

This is shorthand for 1 ;; -2 which means elements one through two from the end.

The second specification is:

2 ;;

This is shorthand for 2 ;; All which means elements two through the end.

For an abstraction of Part to simultaneously delete arbitrary rows and columns see:

Drop

Drop, while not capable of deleting arbitrary rows (or with effort, columns) like Delete, can accept an interval parameter (as can Part by way of Span) to e.g. drop every other element, and it can operate on rows and columns simultaneously like Part. An example of both:

Drop[m, None, {1, -1, 2}]

$\left( \begin{array}{cc} 2 & 4 \\ 4 & 8 \\ 6 & 12 \\ 8 & 16 \\ \end{array} \right)$

For the question application "delete the last row and first column" it is very concise:

Drop[m, -1, 1]

$\left( \begin{array}{cccc} 2 & 3 & 4 & 5 \\ 4 & 6 & 8 & 10 \\ 6 & 9 & 12 & 15 \\ \end{array} \right)$

Delete

You can also use Delete in many cases, however Delete does not support All like Part does which makes it less efficient for column operations, as as one must either Map the function or Transpose the array.

Delete[#, 1] & /@ Delete[m, -1]

$\left( \begin{array}{cccc} 2 & 3 & 4 & 5 \\ 4 & 6 & 8 & 10 \\ 6 & 9 & 12 & 15 \\ \end{array} \right)$

Delete[Delete[m, -1]\[Transpose], 1]\[Transpose]

$\left( \begin{array}{cccc} 2 & 3 & 4 & 5 \\ 4 & 6 & 8 & 10 \\ 6 & 9 & 12 & 15 \\ \end{array} \right)$

However unlike Drop it is not limited to starting, trailing, or evenly spaced elements and allow for e.g.:

Delete[m, {{1}, {3}}]

$\left( \begin{array}{ccccc} 2 & 4 & 6 & 8 & 10 \\ 4 & 8 & 12 & 16 & 20 \\ \end{array} \right)$

When you wish to work with columns it is easier and more efficient to use the Part abstraction referenced earlier.

$\endgroup$
0
5
$\begingroup$

A barebones example for the desired cropping is:

m = Array[Subscript[a, ##] &, {4, 4}];
MatrixForm[m]

$$\left( \begin{array}{cccc} a_{1,1} & a_{1,2} & a_{1,3} & a_{1,4} \\ a_{2,1} & a_{2,2} & a_{2,3} & a_{2,4} \\ a_{3,1} & a_{3,2} & a_{3,3} & a_{3,4} \\ a_{4,1} & a_{4,2} & a_{4,3} & a_{4,4} \\ \end{array} \right)$$

ArrayPad[m, {{0, -1}, {-1, 0}}]

$$\left( \begin{array}{ccc} a_{1,2} & a_{1,3} & a_{1,4} \\ a_{2,2} & a_{2,3} & a_{2,4} \\ a_{3,2} & a_{3,3} & a_{3,4} \\ \end{array} \right)$$

Here, I use the fact that ArrayPad permits zero or negative values of the padding, so one can use it to shrink matrices arbitrarily on all sides.

Also, slick way to do the cropping on all sides of a matrix is this:

ArrayPad[m, -1]

$$\left( \begin{array}{cc} a_{2,2} & a_{2,3} \\ a_{3,2} & a_{3,3} \\ \end{array} \right)$$

$\endgroup$
5
$\begingroup$

Seeing you asked about how to drop the first or last rows/columns, you can do that conveniently with Most and Rest:

Most@m (* drops last row *)
Most/@m (* drops last column *)
Rest@m (* drops first row *)
Rest/@m (* drops first column *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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