Skip to main content
deleted 124 characters in body
Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929
array  = ArrayReshape[Range[5 7], {5, 7}];
Fold[ArrayPad, array, {-1, 1}] // MatrixForm // TeXForm 

$\left( \begin{array}{ccccccc} 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 9 & 10 & 11 & 12 & 13 & 0 \\ 0 & 16 & 17 & 18 & 19 & 20 & 0 \\ 0 & 23 & 24 & 25 & 26 & 27 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ \end{array} \right)$

Also

ArrayPad[ArrayPad[array, -1], 1] // MatrixForm // TeXForm  
ArrayPad[array[[2 ;; -2, 2 ;; -2]], 1] // MatrixForm // TeXForm
array (1 - MorphologicalPerimeter[array]) // MatrixForm // TeXForm
MapAt[0 &, array, {{All, {1, -1}}, {{1, -1}, All}}] //  MatrixForm // TeXForm 
ReplacePart[array, {1 | Dimensions[array][[1]], _} | {_, 1 | Dimensions[array][[2]]} :> 0]
  // MatrixForm // TeXForm

all give the same result.

array  = ArrayReshape[Range[5 7], {5, 7}];
Fold[ArrayPad, array, {-1, 1}] // MatrixForm // TeXForm 

$\left( \begin{array}{ccccccc} 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 9 & 10 & 11 & 12 & 13 & 0 \\ 0 & 16 & 17 & 18 & 19 & 20 & 0 \\ 0 & 23 & 24 & 25 & 26 & 27 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ \end{array} \right)$

Also

ArrayPad[ArrayPad[array, -1], 1] // MatrixForm // TeXForm  
ArrayPad[array[[2 ;; -2, 2 ;; -2]], 1] // MatrixForm // TeXForm
array (1 - MorphologicalPerimeter[array]) // MatrixForm // TeXForm
MapAt[0 &, array, {{All, {1, -1}}, {{1, -1}, All}}] //  MatrixForm // TeXForm 
ReplacePart[array, {1 | Dimensions[array][[1]], _} | {_, 1 | Dimensions[array][[2]]} :> 0]
  // MatrixForm // TeXForm

all give the same result.

array  = ArrayReshape[Range[5 7], {5, 7}];
Fold[ArrayPad, array, {-1, 1}] // MatrixForm // TeXForm 

$\left( \begin{array}{ccccccc} 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 9 & 10 & 11 & 12 & 13 & 0 \\ 0 & 16 & 17 & 18 & 19 & 20 & 0 \\ 0 & 23 & 24 & 25 & 26 & 27 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ \end{array} \right)$

Also

ArrayPad[ArrayPad[array, -1], 1] 
ArrayPad[array[[2 ;; -2, 2 ;; -2]], 1] 
array (1 - MorphologicalPerimeter[array]) 
MapAt[0 &, array, {{All, {1, -1}}, {{1, -1}, All}}] 
ReplacePart[array, {1 | Dimensions[array][[1]], _} | {_, 1 | Dimensions[array][[2]]} :> 0]
  

all give the same result.

added 72 characters in body
Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929
array  = ConstantArray[1ArrayReshape[Range[5 7], {5, 57}];
Fold[ArrayPad, array, {-1, 1}] // MatrixForm // TeXForm 

$\left( \begin{array}{ccccc} 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 1 & 1 & 0 \\ 0 & 1 & 1 & 1 & 0 \\ 0 & 1 & 1 & 1 & 0 \\ 0 & 0 & 0 & 0 & 0 \\ \end{array} \right)$$\left( \begin{array}{ccccccc} 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 9 & 10 & 11 & 12 & 13 & 0 \\ 0 & 16 & 17 & 18 & 19 & 20 & 0 \\ 0 & 23 & 24 & 25 & 26 & 27 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ \end{array} \right)$

Also

ArrayPad[ArrayPad[array, -1], 1] // MatrixForm // TeXForm  
ArrayPad[array[[2 ;; -2, 2 ;; -2]], 1] // MatrixForm // TeXForm
array (1 - MorphologicalPerimeter[array]) // MatrixForm // TeXForm
MapAt[0 &, array, {{All, {1, -1}}, {{1, -1}, All}}] //  MatrixForm // TeXForm 
ReplacePart[array, {1 | Dimensions[array][[1]], _} | {_, 1 | Dimensions[array][[2]]} :> 0]
  // MatrixForm // TeXForm

all give the same result.

$\left( \begin{array}{ccccc} 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 1 & 1 & 0 \\ 0 & 1 & 1 & 1 & 0 \\ 0 & 1 & 1 & 1 & 0 \\ 0 & 0 & 0 & 0 & 0 \\ \end{array} \right)$

array = ConstantArray[1, {5, 5}];
Fold[ArrayPad, array, {-1, 1}] // MatrixForm // TeXForm 

$\left( \begin{array}{ccccc} 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 1 & 1 & 0 \\ 0 & 1 & 1 & 1 & 0 \\ 0 & 1 & 1 & 1 & 0 \\ 0 & 0 & 0 & 0 & 0 \\ \end{array} \right)$

Also

ArrayPad[ArrayPad[array, -1], 1] // MatrixForm // TeXForm  
ArrayPad[array[[2 ;; -2, 2 ;; -2]], 1] // MatrixForm // TeXForm
MapAt[0 &, array, {{All, {1, -1}}, {{1, -1}, All}}] //  MatrixForm // TeXForm 
ReplacePart[array, {1 | Dimensions[array][[1]], _} | {_, 1 | Dimensions[array][[2]]} :> 0]
  // MatrixForm // TeXForm

all give

$\left( \begin{array}{ccccc} 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 1 & 1 & 0 \\ 0 & 1 & 1 & 1 & 0 \\ 0 & 1 & 1 & 1 & 0 \\ 0 & 0 & 0 & 0 & 0 \\ \end{array} \right)$

array  = ArrayReshape[Range[5 7], {5, 7}];
Fold[ArrayPad, array, {-1, 1}] // MatrixForm // TeXForm 

$\left( \begin{array}{ccccccc} 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 9 & 10 & 11 & 12 & 13 & 0 \\ 0 & 16 & 17 & 18 & 19 & 20 & 0 \\ 0 & 23 & 24 & 25 & 26 & 27 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ \end{array} \right)$

Also

ArrayPad[ArrayPad[array, -1], 1] // MatrixForm // TeXForm  
ArrayPad[array[[2 ;; -2, 2 ;; -2]], 1] // MatrixForm // TeXForm
array (1 - MorphologicalPerimeter[array]) // MatrixForm // TeXForm
MapAt[0 &, array, {{All, {1, -1}}, {{1, -1}, All}}] //  MatrixForm // TeXForm 
ReplacePart[array, {1 | Dimensions[array][[1]], _} | {_, 1 | Dimensions[array][[2]]} :> 0]
  // MatrixForm // TeXForm

all give the same result.

added 65 characters in body
Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929
array = ConstantArray[1, {5, 5}];
Fold[ArrayPad, array, {-1, 1}] // MatrixForm // TeXForm 

$\left( \begin{array}{ccccc} 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 1 & 1 & 0 \\ 0 & 1 & 1 & 1 & 0 \\ 0 & 1 & 1 & 1 & 0 \\ 0 & 0 & 0 & 0 & 0 \\ \end{array} \right)$

Also

ArrayPad[ArrayPad[array, -1], 1] // MatrixForm // TeXForm  (*
ArrayPad[array[[2 or;; *)-2, 2 ;; -2]], 1] // MatrixForm // TeXForm
MapAt[0 &, array, {{All, {1, -1}}, {{1, -1}, All}}] //  MatrixForm // TeXForm (* or *)
ReplacePart[array, {1 | Dimensions[array][[1]], _} | {_, 1 | Dimensions[array][[2]]} :> 0]
  // MatrixForm // TeXForm

all give

$\left( \begin{array}{ccccc} 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 1 & 1 & 0 \\ 0 & 1 & 1 & 1 & 0 \\ 0 & 1 & 1 & 1 & 0 \\ 0 & 0 & 0 & 0 & 0 \\ \end{array} \right)$

array = ConstantArray[1, {5, 5}];
Fold[ArrayPad, array, {-1, 1}] // MatrixForm // TeXForm 

$\left( \begin{array}{ccccc} 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 1 & 1 & 0 \\ 0 & 1 & 1 & 1 & 0 \\ 0 & 1 & 1 & 1 & 0 \\ 0 & 0 & 0 & 0 & 0 \\ \end{array} \right)$

Also

ArrayPad[ArrayPad[array, -1], 1] // MatrixForm // TeXForm  (* or *)
MapAt[0 &, array, {{All, {1, -1}}, {{1, -1}, All}}] //  MatrixForm // TeXForm (* or *)
ReplacePart[array, {1 | Dimensions[array][[1]], _} | {_, 1 | Dimensions[array][[2]]} :> 0]
  // MatrixForm // TeXForm

$\left( \begin{array}{ccccc} 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 1 & 1 & 0 \\ 0 & 1 & 1 & 1 & 0 \\ 0 & 1 & 1 & 1 & 0 \\ 0 & 0 & 0 & 0 & 0 \\ \end{array} \right)$

array = ConstantArray[1, {5, 5}];
Fold[ArrayPad, array, {-1, 1}] // MatrixForm // TeXForm 

$\left( \begin{array}{ccccc} 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 1 & 1 & 0 \\ 0 & 1 & 1 & 1 & 0 \\ 0 & 1 & 1 & 1 & 0 \\ 0 & 0 & 0 & 0 & 0 \\ \end{array} \right)$

Also

ArrayPad[ArrayPad[array, -1], 1] // MatrixForm // TeXForm  
ArrayPad[array[[2 ;; -2, 2 ;; -2]], 1] // MatrixForm // TeXForm
MapAt[0 &, array, {{All, {1, -1}}, {{1, -1}, All}}] //  MatrixForm // TeXForm 
ReplacePart[array, {1 | Dimensions[array][[1]], _} | {_, 1 | Dimensions[array][[2]]} :> 0]
  // MatrixForm // TeXForm

all give

$\left( \begin{array}{ccccc} 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 1 & 1 & 0 \\ 0 & 1 & 1 & 1 & 0 \\ 0 & 1 & 1 & 1 & 0 \\ 0 & 0 & 0 & 0 & 0 \\ \end{array} \right)$

added 90 characters in body
Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929
Loading
added 83 characters in body
Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929
Loading
Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929
Loading