Questions tagged [matrix]
This tag is for challenges involving matrices. A matrix, also known as a 2D array, is a list of numbers arranged in a rectangle with rows and columns.
295
questions
101
votes
23
answers
17k
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:
...
99
votes
27
answers
7k
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 ...
75
votes
13
answers
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 ...
61
votes
20
answers
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....
53
votes
12
answers
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 ...
48
votes
106
answers
6k
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 <...
48
votes
16
answers
6k
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 ...
48
votes
14
answers
5k
views
In Flanders Fields
Today is November 11th, which for many regions is Remembrance Day.
If you don't know anything about the history or context of Remembrance Day, you can read about it in the wiki page, but it's not ...
45
votes
21
answers
2k
views
Summon the sevens
You are given a \$3\times3\$ square matrix where each cell is any digit between \$0\$ and \$9\$ except \$7\$. Your task is to figure out the minimum number of digits that must be replaced with \$7\$'s ...
41
votes
51
answers
7k
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 ...
41
votes
33
answers
4k
views
Remove surrounding zeroes of a 2d array
This is a 2-dimensional version of this question.
Given a non-empty 2-dimensional array/matrix containing only non-negative integers:
$$ \begin{bmatrix}
{\color{Red}0} & {\color{Red}0} & {\...
40
votes
13
answers
5k
views
Recreate Minecraft's lighting
Minecraft has a fairly unique lighting system. Each block's light value is either one less than the brightest one surrounding it, or it is a light source itself. Your task is to write a method that ...
38
votes
25
answers
6k
views
I transpose the source code, you transpose the input!
Rip-off of a rip-off of a rip-off of a rip-off. Go upvote those!
Your task, if you wish to accept it, is to write a program/function that outputs/returns its input/argument¹. The tricky part is that ...
37
votes
39
answers
4k
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 ...
37
votes
28
answers
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] \...
36
votes
40
answers
4k
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:
* * * *...
36
votes
28
answers
4k
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'...
35
votes
5
answers
2k
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 ...
34
votes
32
answers
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
15
answers
4k
views
Fryer simulator
Your task is to model the effects of batter on pieces of food. Please add three layers of crust.
...
32
votes
59
answers
3k
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:
...
32
votes
34
answers
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 ...
32
votes
6
answers
2k
views
Slash the matrix
You are given a matrix of forward and back slashes, for instance:
//\\
\//\
//\/
A slash cuts along the diagonal of its cell corner-to-corner, splitting it in ...
31
votes
55
answers
2k
views
Generate this number table
Given an integer \$1 < n < 10 \$ generate a table like below.
For \$n = 5\$,
1 2 3 4 5
2 2 3 4 5
3 3 3 4 5
4 4 4 4 5
5 5 5 5 5
For \$n = 8\$,
...
31
votes
4
answers
1k
views
Expand a recursive pattern
Given a description of the base state of a recursive ASCII pattern, output an expanded state somewhere along the recursion steps.
More specifically: Let the following be an example:
...
30
votes
19
answers
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 ...
30
votes
9
answers
5k
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 ...
29
votes
23
answers
3k
views
Calculate the inverse of a matrix
Somehow, we don't yet have a challenge for finding the inverse of an arbitrarily-sized square matrix, despite having ones for 3x3 and 4x4, as well as a more complex version.
Your task is, given a ...
29
votes
31
answers
3k
views
Antisymmetry of a Matrix
A matrix is antisymmetric, or skew-symmetric, if its transpose equals its negative.
The transpose of a matrix can be obtained by reflecting its elements across the main diagonal. Examples of ...
29
votes
35
answers
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 ...
29
votes
23
answers
6k
views
Binary Numbers Magic Trick
The challenge is simply; output the following six 2D integer arrays:
...
29
votes
26
answers
2k
views
Recursive Z-matrix
Originally from a CMC I proposed for the last BMG event
Challenge
Given a non-negative integer \$n\$, create a 2D array of size \$2^n × 2^n\$ which is generated in the following manner:
Divide the ...
29
votes
17
answers
5k
views
Implement the Max-Pooling operation from Convolutional Neural Networks
In convolutional neural networks, one of the main types of layers usually implemented is called the Pooling Layer. Sometimes, the input image is big (and therefore time consuming especially if you ...
29
votes
26
answers
2k
views
Vandermonde Determinant
Given a vector of \$n\$ values \$(x_1,x_2,x_3,\ldots,x_n)\$ return the determinant of the corresponding Vandermonde matrix
\$V(x_1, x_2, \ldots, x_n) = \begin{bmatrix}1 & x_1 & x_1^2 & x_1^...
29
votes
10
answers
2k
views
Help Stack Exchange transpose tables
Stack Exchange doesn't know how to transpose tables. Let's help.
Given a markdown table, transpose it.
Input assumptions:
There will be at least two rows (including header) and two columns
Either all ...
29
votes
9
answers
2k
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 ...
29
votes
7
answers
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
35
answers
2k
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 ...
27
votes
45
answers
3k
views
Output a Latin Square
An \$n\times n\$ Latin Square is a grid containing exactly \$n\$ distinct values where the values in each row and column are distinct. For example,
$$\begin{matrix}
A & B & C \\
C & A &...
27
votes
58
answers
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 ...
27
votes
22
answers
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 ...
27
votes
19
answers
3k
views
Create Bernard from Desmos
Background
In the online graphing calculator Desmos, there is a certain shape that appears in the lower left portion of the graph in many high detail graphs, which the Desmos community has dubbed &...
27
votes
21
answers
4k
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 ...
27
votes
9
answers
5k
views
Implement the 2D Hadamard Transform
The Hadamard transform works similarly to the Fourier transform: it takes a vector and maps it to its frequency components, which are the Walsh functions. Instead of sine waves in the Fourier ...
27
votes
10
answers
4k
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 ...
27
votes
8
answers
584
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:
...
27
votes
3
answers
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 ...
26
votes
19
answers
3k
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 ...
26
votes
21
answers
2k
views
Logo Pack LAPACK
This is the logo for LAPACK, a widely used software library for numerical linear algebra.
Your task is to print the logo (color and matrix brackets not required) as this exact string. Trailing ...
26
votes
21
answers
3k
views
I'm Lazy*: Top-left align my text
* and don't have a word processor with top-left align support :D
Take several lines of input, with at least four unique characters of your choice including newline and space. The input can also be ...