25
1
For each row and then column of a matrix, we can add an extra entry with the sum of the last two entries in that row or column. For example with the following input matrix:
[ 1 1 1 ]
[ 2 3 4 ]
The resulting matrix would be:
[ 1 1 1 2 ]
[ 2 3 4 7 ]
[ 3 4 5 9 ]
Given an input of an integer N and an [X,Y] matrix of size at least 2x2, perform the above expansion N times and output the result. The resulting matrix will always be of size [X+N,Y+N].
Examples:
Input: Output:
2, [ 0 0 ] [ 0 0 0 0 ]
[ 0 0 ] [ 0 0 0 0 ]
[ 0 0 0 0 ]
[ 0 0 0 0 ]
3, [ 1 1 1 ] [ 1 1 1 2 3 5 ]
[ 2 3 4 ] [ 2 3 4 7 11 18 ]
[ 3 4 5 9 14 23 ]
[ 5 7 9 16 25 41 ]
[ 8 11 14 25 39 64 ]
1I don't know MATL, but wouldn't it be shorter to loop
2N
times than to loop twiceN
times? – Zgarb – 2016-02-05T00:25:07.467@Zgarb Of course! How did I miss that? Thanks!! – Luis Mendo – 2016-02-05T00:42:03.013
Does MATL have a built-in for doubling a number? – Zgarb – 2016-02-05T00:46:19.383
@Zgarb No. You need
2*
(postfix notation). Maybe it should have a one-character built-in, it's used often. Also2^
(square). But I'm running out of code space :-) – Luis Mendo – 2016-02-05T00:49:08.307