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
:
- Highly efficient
- Can select (or drop) rows and columns at the same time
- 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.