Questions tagged [matrix]

A matrix is a list of numbers arranged in a rectangle with rows and columns. In programming, it is also known as a 2D array. Use this tag if your challenge is about manipulating matrices.

90
votes
20answers
14k views

Where's the 0xBEEF?

This challenge was inspired by this Wendy's commercial from 1984. Illustration by T S Rogers Your task is to find a hexadecimal 0xBEEF on a binary bun. The 'beef' consists of the following pattern: ...
83
votes
25answers
6k views

The Hungry Mouse

Sixteen piles of cheese are put on a 4x4 square. They're labeled from \$1\$ to \$16\$. The smallest pile is \$1\$ and the biggest one is \$16\$. The Hungry Mouse is so hungry that it always goes ...
72
votes
13answers
4k views

Friar simulator

The brethren from the order of St Golfus the Concise have a tradition of reciting a short prayer whenever they see someone make the sign of the cross. Due to the high level of sin measured among ...
58
votes
20answers
6k views

Dig a border trench

Background: Too many illegal immigrants from Blandia are crossing the border to Astan. The emperor of Astan has tasked you with digging a trench to keep them out, and Blandia must pay for the expenses....
52
votes
12answers
7k views

Seats in a Finnish cinema

You're given the map of a cinema theatre as a boolean matrix: 0 represents a free seat, 1 - occupied. Each Finn who walks in chooses the seat farthest away (Euclidean distance) from the nearest ...
43
votes
84answers
5k views

Construct the Identity Matrix

The challenge is very simple. Given an integer input n, output the n x n identity matrix. The identity matrix is one that has <...
43
votes
10answers
4k views

Zigzagify a Matrix

As part of its compression algorithm, the JPEG standard unrolls a matrix into a vector along antidiagonals of alternating direction: Your task is to take a matrix (not necessarily square) and return ...
40
votes
51answers
6k views

Cops: Make a regex - Make a snake

This is the cop's thread. The robber's thread is here. Write a code that takes an input n and creates a n-by-n "snake matrix". A snake matrix is a matrix that ...
37
votes
28answers
4k views

Is the matrix centrosymmetric… and so is the code?

Defintion A centrosymmetric matrix is a square matrix that is symmetric about its center. More rigorously, a matrix \$A\$ of size \$n \times n\$ is centrosymmetric if, for any \$i,\: j \in ([1, n] \...
34
votes
37answers
3k views

Determinant of an Integer Matrix

Given a square integer matrix as input, output the determinant of the matrix. Rules You may assume that all elements in the matrix, the determinant of the matrix, and the total number of elements in ...
33
votes
31answers
5k views

Make it explode!

Take a matrix of positive integers as input, and make it explode! The way you explode a matrix is by simply adding zeros around every element, including the outside borders. Input/output formats are ...
33
votes
40answers
3k views

Is my Matrix Arrowhead?

Definition An arrowhead matrix is a matrix that has all entries equal to 0, except the ones on the main diagonal, top row and leftmost column. In other words, the matrix should look like this: * * * ...
31
votes
26answers
2k views

Sum of first row and column, then second row and column … and so on

Take a non-empty matrix / numeric array containing positive integers as input. Return, in this order, the sums of the first row and column, then the second row and column and continue until there aren'...
31
votes
13answers
4k views

Fryer simulator

Your task is to model the effects of batter on pieces of food. Please add three layers of crust. ...
30
votes
33answers
3k views

Rebuild a rectangular array from a corner

I once had a beautiful rectangular array. It was very symmetrical, but unfortunately it has fallen apart and now I only have the top left corner. Your task will be to rebuild the original array. Your ...
30
votes
4answers
1k views

Rotating a 2D Matrix

Let's say I have the following (2D) matrix: [[1, 2, 3, 4 ], [5, 6, 7, 8 ], [9, 10, 11, 12], [13, 14, 15, 16]] Rotate the matrix counterclockwise ...
29
votes
7answers
2k views

We ended up where we started!…

Challenge: Input: You take two inputs: - A string only containing printable ASCII (excluding spaces, tabs or new-lines) - A printable ASCII character Output: The first line will contain the string-...
28
votes
9answers
1k views

Fire propagation simulator

Suppose we have a matrix like this: 11111 12221 12321 12221 11111 This matrix represents a terrain, and each cell represents a portion of terrain. The number in ...
27
votes
22answers
4k views

Binary Numbers Magic Trick

The challenge is simply; output the following six 2D integer arrays: ...
26
votes
51answers
3k views

Orthodiagonal steps

It's a common problem to navigate in a 2D matrix. We've seen it many times and will see again. So let's help future us and develop the shortest solutions to generate all eight possible steps in a 2D ...
26
votes
47answers
2k views

Create a checkerboard matrix

Take a positive integer n as input, and output a n-by-n checkerboard matrix consisting of 1 and 0. The top left digit should always be 1. Test cases: ...
26
votes
22answers
6k views

Is it a good 2048 board?

This is my first question here, so any suggestions in the comments would be appreciated! Thanks ;) Introduction One very common strategy for the 2048 game is never ever swiping down. This positions ...
26
votes
29answers
2k views

Rotation summation

Take a square matrix containing positive integers as input, and calculate the "rotated sum" of the matrix. Rotated sum: Take the sum of the original matrix and the same matrix rotated 90, 180 and ...
26
votes
32answers
1k views

Index of the row with most non-zero elements

This is a simple one: Take a matrix of integers as input, and output the index of the row with the most non-zero elements. You may assume that there will only be one row with the most non-zero ...
26
votes
16answers
4k views

Roman Army Shields

Sandbox post (deleted) The old roman army formations are very famous around the world. In these formations roman legionaries grouped in a geometric shape (usually a rectangle) protecting the flanks ...
26
votes
16answers
3k views

Beware of the matrix tornado!

The matrix tornado is just like any other tornado: it consists of things rotating around a center. In this case, elements of the matrix instead of air. Here is an example of a matrix tornado: First ...
26
votes
9answers
3k views

Calculate the permanent as quickly as possible

The challenge is to write the fastest code possible for computing the permanent of a matrix. The permanent of an n-by-n matrix ...
26
votes
10answers
3k views

Symbolic matrix multiplication

There are lots of different ways to explain matrix multiplication. I'll stick with a single figure since I believe most people here are familiar with it (and the figure is very descriptive). If you ...
26
votes
12answers
2k views

Find arsonist's lullaby

Imagine an arsonist walking around the town and picking its victims according to a very specific pattern (Or, alternatively, Imagine a bee flying around the garden and picking its flowers to pollenize ...
26
votes
3answers
1k views

Golf A Free Lunch

Find a maximally profitable sequence of exchanges given an exchange rate table. As an example consider the currencies Ariary (your home currency), Baht, Cedi, and Denar where the rate from one to ...
25
votes
17answers
976 views

Vandermonde Determinant

Given a vector of n values (x1,x2,x3,...,xn) return the determinant of the corresponding Vandermonde matrix. This determinant ...
25
votes
8answers
2k views

Exploding Numbers

sandbox (deleted) Lets define a matrix of 9s as: $$ N = \begin{bmatrix} 9&9&9\\9&9&9\\9&9&9 \end{bmatrix} $$ Lets define an exploding number as a number at position \$(x,y)\$...
25
votes
14answers
1k views

Fibonacci-style matrix expansion

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: ...
25
votes
12answers
1k views

Draw my Contours

Given a rectangular matrix of elevations, draw its contours. Task Two elements x and y are on the same contour level if ...
25
votes
11answers
2k views

Is this Pascal's Matrix?

In Pascal's triangle each number is the sum of the two numbers directly above it, treating empty spots as zero: By rotating the triangle, we can cut out square matrices of varying sizes and rotations ...
25
votes
4answers
471 views

Interlaced Rotations

Challenge Given a square matrix of characters (single-byte printable ASCII characters), rotate each "ring" of the matrix in opposite directions. Let's take an example: ...
24
votes
17answers
2k views

Is it a stochastic matrix?

A stochastic matrix is a matrix of probabilities used in the context of Markov chains. A right stochastic matrix is a matrix where each row sums to 1. A left ...
24
votes
15answers
3k views

Matrix Jigsaw Puzzles

Input: An integer n Two equal-sized square matrices (with their width/height being a multiple of n) Output: One of two ...
23
votes
21answers
3k views

Matrix in “slash” order

Given two positive numbers N >= 2 and N <= 100 create a matrix which follows the following rules: First Number starts at ...
23
votes
27answers
2k views

Generalized matrix trace

Inspiration. Given (by any means): A two-argument (or single argument consisting of a two-element list) black box function, f: ℤ+ × ℤ+ → ℤ+ (input and output are ...
23
votes
16answers
2k views

Create a pyramidal matrix

A pyramidal matrix is a square matrix where all numbers increase or decrease from the center point, like the two matrices below: ...
23
votes
12answers
1k views

Pad a matrix with its sums

Challenge: Given a square input matrix A, pad the matrix with one row and one column on all four sides. The value of each element in the top and bottom row should be the sum of the elements in each ...
22
votes
16answers
3k views

Find the inverse of a 3 by 3 matrix

Challenge Given nine numbers, a, b, c, d, e, f, g, h, i, as input which correspond to the square matrix: $$\mathbf{M} = \begin{pmatrix}a& b& c\\ d& e&...
22
votes
17answers
2k views

Generate a Walsh Matrix

A Walsh matrix is a special kind of square matrix with applications in quantum computing (and probably elsewhere, but I only care about quantum computing). Properties of Walsh matrices The ...
22
votes
13answers
2k views

Codegolf the Hafnian

The challenge is to write codegolf for the Hafnian of a matrix. The Hafnian of an 2n-by-2n symmetric matrix ...
22
votes
3answers
603 views

Minimum rectangle cover

Rectangle covers Suppose you have a matrix of bits, for example the following. 1 1 0 0 0 1 1 0 1 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 1 1 0 1 1 1 1 0 1 1 0 1 1 1 0 1 ...
21
votes
18answers
3k views

Is this a valid Takuzu board?

Takuzu is a logic game in which you have to complete a grid with cells containing 0s and 1s. The grid must follow 3 rules: No ...
21
votes
14answers
1k views

Find the submatrix with the smallest mean

You're given a n-by-m matrix of integers, where n,m > 3. Your task is to find the 3-by-3 sub-matrix that has the lowest mean, and output this value. Rules and clarifications: The integers will be ...
21
votes
21answers
2k views

Overlapping String-Blocks

Challenge: Given a list of multi-line strings, overlap them (in the top-left) and output the result. Example: Input: ["aaaa\naaaa\naaaa\naaaa","bb\nbb\nbb","c"] ...
21
votes
16answers
2k views

Verify Eigenpairs

In this challenge, you will be given a square matrix A, a vector v, and a scalar λ. You will ...