3
$\begingroup$

This question already has an answer here:

I have some large matrices, which I need to remove the first row and first column (both of which are full of zeroes). In this sense I need the inverse of "ArrayPad".

For example, consider the matrix:

A = {{0, 0, 0, 0}, {0, 1, 2, 3}, {0, 4, 5, 6} , {0, 7, 8, 9}}
A // MatrixForm

I would like an operation to reduce it to

Anew =  {{1, 2, 3}, {4, 5, 6} , {7, 8, 9}}
Anew // MatrixForm

I need general recipe for such operation for to go from (N+1) by (N+1) matrix to N by N matrix by removing the first column of zeroes and first row of zeroes.

Any help appreciated.

$\endgroup$

marked as duplicate by Feyre, Wjx, happy fish, Mr.Wizard Sep 4 '16 at 14:30

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 3
    $\begingroup$ A[[2;;,2;;]] 15char $\endgroup$ – LLlAMnYP Sep 3 '16 at 11:17
  • 2
    $\begingroup$ f=Drop[#,1]& drops the first element of a list. f=Drop[#,-1]& drops the last. f[A] removes rows. f/@A removes columns. $\endgroup$ – mikado Sep 3 '16 at 11:31
  • $\begingroup$ This question is answered in: (17002), (20228), (76109) $\endgroup$ – Mr.Wizard Sep 4 '16 at 14:30
4
$\begingroup$

Rest is the built-in function for removing the first element from a list, so given

m = {{0, 0, 0, 0}, {0, 1, 2, 3}, {0, 4, 5, 6}, {0, 7, 8, 9}};

then

Rest[Rest /@ m]

produces

{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}

as does

Rest /@ Rest[m]
$\endgroup$
4
$\begingroup$
m0 =  {{1,2,3},{4,5,6},{7,8,9}};
m1 = ArrayPad[m0, {{1,0},{1,0}}]

{{0,0,0,0}, {0,1,2,3},{0,4,5,6},{0, 7,8,9}}

m2 = ArrayPad[m1, -{{1,0},{1, 0}}]

{{1,2,3},{4,5,6},{7,8,9}}

$\endgroup$
  • 2
    $\begingroup$ Weird! Probably the closest fit to the description "inverse of ArrayPad" here. $\endgroup$ – Lucas Sep 5 '16 at 6:10
3
$\begingroup$

You can use the [[ ]] notation, which is backed by Part. You just need to start from an index of 2. i.e.

A[[2 ;;, 2 ;;]]

For example, if we have a matrix x given by

x = Table[j + 4 i + 1, {i, 0, 3}, {j, 0, 3}];
x // MatrixForm

$\left( \begin{array}{cccc} 1 & 2 & 3 & 4 \\ 5 & 6 & 7 & 8 \\ 9 & 10 & 11 & 12 \\ 13 & 14 & 15 & 16 \\ \end{array} \right)$

Then the operation

x[[2 ;;, 2 ;;]]

gives

% // MatrixForm

$\left( \begin{array}{ccc} 6 & 7 & 8 \\ 10 & 11 & 12 \\ 14 & 15 & 16 \\ \end{array} \right)$

$\endgroup$

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