# Transform a matrix

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


Output:

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


Rules:

• As input use integers.
• This is a , so the shortest code in bytes wins.
• 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? – Doorknob Apr 26 '16 at 21:05
• Thank you. I guess up to, and yes a number will be surrounded by negative numbers. – M.T Apr 26 '16 at 21:08
• In what ways can we take input? – Maltysen Apr 26 '16 at 21:14
• @Doorknob The sum of an empty set of numbers is 0. – orlp Apr 26 '16 at 21:16
• @Maltysen STDIN, I guess. – M.T Apr 26 '16 at 21:17

# MATL, 9 bytes

t0>*1Y6Z+


Try it online!

### Explanation

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

• Are phones allowed as I/O? – orlp Apr 26 '16 at 21:28
• Well then, I doubt it if this is beatable :p – Adnan Apr 26 '16 at 21:28
• @Adnan In Python it's not – R. Kap Apr 26 '16 at 21:29
• 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... – R. Kap Apr 26 '16 at 21:30
• @R.Kap I meant "typed from the phone" (not "typed while talking on the phone"). Sorry about my English :-) – Luis Mendo Apr 26 '16 at 22:47

# Octave, 4644 40 bytes

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

@(M)conv2(M.*(M>0),(x='aba')~=x','same')


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

You can see it here on ideone.

• COMON, I was just about to post this exact answer. – flawr Apr 26 '16 at 22:10
• You can use save 6 bytes using (x='aba')~=x' instead of [0 1 0;1 0 1;0 1 0] – Luis Mendo Apr 26 '16 at 22:28
• @LuisMendo What is this witchcraft? – beaker Apr 26 '16 at 22:33
• @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. – beaker Apr 26 '16 at 23:49
• (x='aba')~=x'. That's just awesome @Luis! – Stewie Griffin Apr 27 '16 at 12:03

## JavaScript (ES6), 99 94 bytes

a=>a.map((b,i)=>b.map((_,j)=>(g=(c=j,k=j)=>c[k]>0&&c[k])(a[i-1])+g(a[i+1])+g(b,j-1)+g(b,j+1)))


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.

• – ascx Apr 27 '16 at 8:13
• @Socialz Neils version is older. – flawr Apr 27 '16 at 10:59
• @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. – ascx Apr 27 '16 at 11:29
• So you basically want to advertise the other post?? – flawr Apr 27 '16 at 11:52
• @Socialz Was 4 bytes longer, yes ;-) – Neil Apr 27 '16 at 12:55

# JavaScript (ES6), 95 93 bytes

document.write("<pre>"+(

m=>m.map((a,r)=>a.map((_,c)=>(s=(x,y=0)=>(n=(m[r+y]||0)[c+x])>0&&n)(1)+s(-1)+s(0,1)+s(0,-1)))

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

.join\n)

## 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

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. 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, 2320 18 bytes

0;+ḊṖ
ZÇ€Z+Ç€
+AHÇ


Try it online!

## Algorithm

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

Lm+Vt+d0+0dbJmm/+.akk2dQ.b+VNYyJCyCJ


Try it online!

Direct translation of my answer in Jelly.