Write a program or function where a matrix A (maximum value of dimensions of the matrix is 100) is transformed to a new matrix B. Each element of the new matrix B is the sum of the positive neighbors of the corresponding element of the matrix A.

Only neighbors in the four orthogonal directions are considered, and the edges do not wrap around.

Example input:

 1  2  3
 4 -1 -2
-3 -4 10
 1  2 10


 6  4  2
 1  6 13
 5 12 10
 2 11 12


  • As input use integers.
  • This is a , so the shortest code in bytes wins.
  • 3
    \$\begingroup\$ Welcome to Programming Puzzles & Code Golf! All challenges here require an objective winning criterion, in order to indisputably determine which solution should win. Typically, this is code-golf, which means that the shortest code in bytes wins. Furthermore, it would be helpful to specify the valid input/output formats (2d array? single string? etc.). Finally, there are some edge cases you haven't covered; for example, will a number ever be surrounded only by negative numbers? \$\endgroup\$
    – Doorknob
    Commented Apr 26, 2016 at 21:05
  • \$\begingroup\$ Thank you. I guess up to[100][100], and yes a number will be surrounded by negative numbers. \$\endgroup\$
    – M.T
    Commented Apr 26, 2016 at 21:08
  • \$\begingroup\$ In what ways can we take input? \$\endgroup\$
    – Maltysen
    Commented Apr 26, 2016 at 21:14
  • 1
    \$\begingroup\$ @Doorknob The sum of an empty set of numbers is 0. \$\endgroup\$
    – orlp
    Commented Apr 26, 2016 at 21:16
  • \$\begingroup\$ @Maltysen STDIN, I guess. \$\endgroup\$
    – M.T
    Commented Apr 26, 2016 at 21:17

8 Answers 8


MATL, 9 bytes


Try it online!


The input matrix is multplied by an appropriate mask to make negative values equal to 0. Then a 2D convolution is applied to compute the sum of neighbours of each entry.

t     % Take input implicitly: 2D array. Duplicate
0>    % Is each entry positive? This gives a mask of positive values
*     % Multiply: set negative values of input array to zero
1Y6   % Predefined literal: [0 1 0; 1 0 1; 0 1 0]
Z+    % 2D convolution preserving size. Implicitly display
  • 4
    \$\begingroup\$ Are phones allowed as I/O? \$\endgroup\$
    – orlp
    Commented Apr 26, 2016 at 21:28
  • \$\begingroup\$ Well then, I doubt it if this is beatable :p \$\endgroup\$
    – Adnan
    Commented Apr 26, 2016 at 21:28
  • \$\begingroup\$ @Adnan In Python it's not \$\endgroup\$
    – R. Kap
    Commented Apr 26, 2016 at 21:29
  • 4
    \$\begingroup\$ This guy did this while on the phone, and here I am, having to dedicate some of my time to trying to solve some of these problems... \$\endgroup\$
    – R. Kap
    Commented Apr 26, 2016 at 21:30
  • \$\begingroup\$ @R.Kap I meant "typed from the phone" (not "typed while talking on the phone"). Sorry about my English :-) \$\endgroup\$
    – Luis Mendo
    Commented Apr 26, 2016 at 22:47

Octave, 46 44 40 bytes

Saved 2 bytes thanks to @flawr
@LuisMendo's kernel was 4 bytes shorter than @flawr's.


Just like @LuisMendo's answer! Only less... golfy.

You can see it here on ideone.

  • \$\begingroup\$ COMON, I was just about to post this exact answer. \$\endgroup\$
    – flawr
    Commented Apr 26, 2016 at 22:10
  • 1
    \$\begingroup\$ You can use save 6 bytes using (x='aba')~=x' instead of [0 1 0;1 0 1;0 1 0] \$\endgroup\$
    – Luis Mendo
    Commented Apr 26, 2016 at 22:28
  • 2
    \$\begingroup\$ @LuisMendo What is this witchcraft? \$\endgroup\$
    – beaker
    Commented Apr 26, 2016 at 22:33
  • 1
    \$\begingroup\$ @cat .* is element-wise matrix multiplication. The boolean matrix is, MATLAB being largely typeless, treated as numeric. So M>0 is just acting as a mask. \$\endgroup\$
    – beaker
    Commented Apr 26, 2016 at 23:49
  • 1
    \$\begingroup\$ (x='aba')~=x'. That's just awesome @Luis! \$\endgroup\$ Commented Apr 27, 2016 at 12:03

JavaScript (ES6), 99 94 bytes


Accepts and returns a two-dimensional array.

Edit: Completely rewritten when I discovered that default arguments work when you pass an explicitly undefined value, such as when you index off the end of an array.

  • \$\begingroup\$ Related: codegolf.stackexchange.com/a/78671/44704 \$\endgroup\$
    – ascx
    Commented Apr 27, 2016 at 8:13
  • \$\begingroup\$ @Socialz Neils version is older. \$\endgroup\$
    – flawr
    Commented Apr 27, 2016 at 10:59
  • \$\begingroup\$ @flawr Some people might be looking at this message chain in vote order, not the post date order, hence I commented that related answer. This one is 4 bytes longer than user's one. \$\endgroup\$
    – ascx
    Commented Apr 27, 2016 at 11:29
  • \$\begingroup\$ So you basically want to advertise the other post?? \$\endgroup\$
    – flawr
    Commented Apr 27, 2016 at 11:52
  • \$\begingroup\$ @Socialz Was 4 bytes longer, yes ;-) \$\endgroup\$
    – Neil
    Commented Apr 27, 2016 at 12:55

JavaScript (ES6), 95 93 bytes



)([[  1,  2,  3 ],
   [  4, -1, -2 ],
   [ -3, -4, 10 ],
   [  1,  2, 10 ]])



Python with SciPy, 127 bytes

from scipy.signal import*
lambda A,r=range(3):convolve2d([[x*(x>0)for x in y]for y in A],[[0,1,0],[1,0,1],[0,1,0]],mode='same')

This computes the result using Luis Mendo's method.

Try it online


Mathcad, bytes

Uses standard 2D convolution of the filtered array with a 3x3 kernel. Variants with negative element sums and diagonal kernel also added as part compensation for the program not being in the running for least bytes.

enter image description here

No byte count entered as Mathcad scoring has yet to be determined. However, using keyboard equivalence, it is the region of 28 bytes assuming that matrix input doesn't count towards the total.

Note that what you see in the image above is exactly how the solutionn is entered and displayed in Mathcad.


Jelly, 23 20 18 bytes


Try it online!


Let's say there is only one row: [1,2,3,4].

Let's say A is the result of prepending a zero, i.e. [0,1,2,3,4].

B is the result of removing the first item, i.e. [2,3,4].

Then the final result is simply A+B vectorized, then removing the last item.

Now, the algorithm is to apply this to every row as well as every column, then find their vectorized sum.

To each column?! I thought Jelly does not support this...

You're right. Therefore, I transposed it, applied to each row, then transposed it again.

Algorithm for removing negative numbers

Here, you just add to each number their absolute. It effectively eliminates negative numbers while doubling each positive number. Then, just halve the whole matrix.


Pyth, 36 bytes


Try it online!

Direct translation of my answer in Jelly.


Your Answer

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

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