6
$\begingroup$

I have to set the first and last rows and columns to zero rows and columns.

I tried with the first column:

For[a = 1, a <= 3, a++, M = ReplacePart[mat, {a, 1} -> 0]; Print[M]]

{{0,1,1},{1,1,1},{1,1,1}

{{1,1,1},{0,1,1},{1,1,1}}

{{1,1,1},{1,1,1},{0,1,1}}

How can I solve this?

$\endgroup$
  • $\begingroup$ Please, read the full documentation on ReplacePart. Also, it's good practice to include self contained examples -- in this case your initialisation of mat is missing. $\endgroup$ – Sektor May 24 '18 at 11:21
7
$\begingroup$
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.

| improve this answer | |
$\endgroup$
  • $\begingroup$ Henrik brought up an important issue on array (un)packing, please see my comment below his answer. $\endgroup$ – LLlAMnYP May 24 '18 at 12:17
  • $\begingroup$ Thank you @LLlAMnYP. $\endgroup$ – kglr May 24 '18 at 12:19
5
$\begingroup$
m = 2000;
n = 1000;
mat = RandomReal[{-1, 1}, {m, n}];

First@RepeatedTiming[


  M = mat;
  M[[{1, -1}, All]] = 0.;
  M[[2 ;; -2, {1, -1}]] = 0.;


  ]

0.0039

| improve this answer | |
$\endgroup$
  • $\begingroup$ Your method doesn't unpack array because you set the elements to real 0.. If you modify kglr's solution to Fold[ArrayPad[##, 0.] &, array, {-1, 1}], it also doesn't unpack. $\endgroup$ – LLlAMnYP May 24 '18 at 12:16
  • $\begingroup$ Yeah, I've just observed that and deleted that remark. It's still about three times faster. $\endgroup$ – Henrik Schumacher May 24 '18 at 12:18
0
$\begingroup$
array = ArrayReshape[Range[5 17], {5, 17}];  // after kglr

Transpose[Transpose[array.DiagonalMatrix[{0, 1, 1, 1, 1, 1, 0}]].DiagonalMatrix[{0, 1,
  1, 1, 0}]] // MatrixForm

Or, more generally:

array // Transpose[Transpose[#.SparseArray[{{1, 1} -> 0, 
ConstantArray[Dimensions[#][[2]], 2] -> 0, Band[{1, 1}] -> 1}, 
Dimensions[#][[2]]]].SparseArray[{{1, 1} -> 0, 
ConstantArray[Dimensions[#][[1]], 2] -> 0, Band[{1, 1}] -> 1}, 
Dimensions[#][[1]]]] & // MatrixForm

$\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)$

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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