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.

Filter by
Sorted by
Tagged with
24 votes
24 answers
3k views

Diamondize a Matrix

Given a matrix, output a representation of the matrix where the top left element is on top, the anti-diagonal is the central row and the bottom right element is at the bottom. For example, consider ...
  • 36.8k
45 votes
11 answers
5k 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 ...
35 votes
38 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 ...
user avatar
20 votes
22 answers
1k views

Simplify matrix input!

I have written a few challenges related to matrices, and common for all are that I use a format like the one below when representing the matrices, both in examples and in test cases: ...
32 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. ...
  • 14.9k
20 votes
14 answers
2k views

Check if all non-zero elements in a matrix are connected

Input: A matrix containing integers in the range [0 - 9]. Challenge: Determine if all non-zero elements are connected to each other vertically and/or horizontally. Output: A truthy value if all ...
23 votes
3 answers
875 views

Reconstruct a zigzagified 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 the unrolled vector along with the matrix ...
3 votes
16 answers
732 views

Draw diagonal positions of me squared

Based on the interpretation user @ThePirateBay made from the first version of my own challenge Output diagonal positions of me squared now I challenge you to make ASCII art that draws diagonal lines ...
  • 3,338
44 votes
90 answers
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 <...
  • 1,317
35 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 ...
25 votes
56 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 ...
  • 3,336
15 votes
4 answers
1k views

Solve a 2x2 Eigensystem

For those with a little linear algebra background, the challenge is as simple as this: determine the eigenvalues and eigenvectors of a given complex 2x2 matrix. You may skip ahead to The Challenge for ...
23 votes
28 answers
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 1,...
  • 28.4k
18 votes
26 answers
2k views

Do Matrix Multiplication!

In mathematics, matrix multiplication or the matrix product is a binary operation that produces a matrix from two matrices. The definition is motivated by linear equations and linear transformations ...
  • 10.4k
20 votes
13 answers
3k views

Codegolf the permanent

The challenge is to write codegolf for the permanent of a matrix. The permanent of an \$n\times n\$ matrix \$A = a_{i,j}\$) is defined as $$\text{perm}(A) = \sum_{\sigma \in S_n} \prod^n_{i=1} a_{i,\...
user avatar
97 votes
27 answers
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 ...
  • 171k
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 ...
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 ...
  • 12.6k
18 votes
5 answers
1k views

Jordan Decomposition

Important note: Because this challenge only applies to square matrices, any time I use the term "matrix", it is assumed that I am referring to a square matrix. I am leaving off the "...
user avatar
41 votes
32 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} & {\...
  • 37.6k
29 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 ...
user avatar
22 votes
25 answers
3k views

Apply gravity to this matrix

This was inspired by this question. Given an \$m\times n\$ matrix of \$0\$'s and \$1\$'s, apply "gravity" to it. This means to drop down all the \$1\$'s as if they were affected by gravity. ...
  • 799
17 votes
10 answers
530 views

Ascending matrix

The "ascending matrix" is an infinite matrix of whole numbers (0 included) in which any element is the smallest available element which has not been previously used on the respective row and column: <...
13 votes
9 answers
2k views

Calculate the Kronecker Product

Related, but very different. In the examples below, \$A\$ and \$B\$ will be \$2\times2\$ matrices, and the matrices are one-indexed. A Kronecker product has the following properties: ...
21 votes
16 answers
2k views

Verify Eigenpairs

In this challenge, you will be given a square matrix A, a vector v, and a scalar λ. You will ...
  • 40.3k
21 votes
22 answers
2k views

Sum the diagonals

Take a matrix of positive integers as input, and output the individual sums of the elements on the diagonal lines through the matrix. You shall only count the lines that goes diagonally down and to ...
19 votes
16 answers
1k views

(RGS 4/5) Inverting matrices modulo m

Task Given an integer matrix M and a modulus m, find an inverse of M modulo ...
  • 13.8k
18 votes
22 answers
1k views

Output diagonal positions of me squared

Given a number n, Output an ordered list of 1-based indices falling on either of the diagonals of an n*n square matrix. ...
  • 3,338
10 votes
7 answers
2k views

There, I fixed it (with rope)

Related: Rather similar (but much easier) challenge horizontally: There I fixed it (with tape) Challenge: Given a string only containing upper- and/or lowercase letters (whichever you prefer) and new-...
5 votes
16 answers
2k views

2D Array Middle Point

Given dimensions of a 2D array, except 1xn, that results in an odd number of elements, find the midpoint index. Example: Input is 3x5, representing the 2D array [0,0] [0,1] [0,2] [0,3] [0,4] [1,0] [1,...
2 votes
1 answer
216 views

Advent Challenge 7: Balance the storage carts!

<< Prev Next >> After some careful analysis, Santa was able to determine the dock size ranges and get the presents into the correct transportation dock. Now, he needs you to help him balance ...
  • 40.3k
100 votes
22 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: ...
  • 171k
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] \...
  • 41.6k
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 ...
  • 14.9k
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 ...
  • 14.9k
29 votes
20 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
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 ...
  • 66.1k
22 votes
17 answers
3k views

Make a Rectangle from a Triangle

You can depict a triangular number, T(N), by writing one 1 on a line, then two 2's on the line below, then three 3's on the line below that, and so on until N N's. You end up with a triangle of T(N) ...
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 ...
  • 14.9k
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 ...
  • 461
29 votes
23 answers
5k views

Binary Numbers Magic Trick

The challenge is simply; output the following six 2D integer arrays: ...
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 ...
23 votes
17 answers
3k 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
13 answers
2k views

Is the matrix rank-one?

Given a matrix of integers, test if it's rank-one, meaning that every row is a multiple of the same vector. For example, in ...
  • 139k
21 votes
12 answers
2k views

Bandwidth of a matrix

A band matrix is a matrix whose non-zero entries fall within a diagonal band, consisting of the main diagonal and zero or more diagonals on either side of it. (The main diagonal of a matrix consists ...
  • 34.4k
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: * * * *...
  • 41.6k
28 votes
48 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: ...
25 votes
4 answers
823 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 ...
  • 42.2k
24 votes
16 answers
4k 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.8k
19 votes
14 answers
1k views

Square-Random-Symmetrical

Challenge Write a program or a function that returns or prints a square-random-symmetrical matrix. Input N: The size of the matrix i.e 6 x 6 Output The matrix. ...
  • 957