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
47 votes
99 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 <...
20 votes
22 answers
1k views

Borders of a Rectangular Matrix

Although it's done a few times as sub-challenge of a larger challenge, and we also have a challenge to remove the borders of a square matrix, I couldn't find a challenge to output the borders of a ...
36 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 ...
10 votes
5 answers
231 views

Calculate the Smith normal form of an integer matrix

Given an \$m \times n\$ matrix of integers A, there exist a \$m \times m\$ matrix P, an \$m \times n\$ matrix D, and an \$n \times n\$ matrix Q such that: \$A = P D Q\$. P and Q are unimodular ...
15 votes
9 answers
441 views

Compute the perimeter density matrix

Introduction The perimeter density matrix is an infinite binary matrix M defined as follows. Consider a (1-based) index (x, y), and denote by M[x, y] the rectangular sub-matrix spanned by the corner (...
16 votes
14 answers
1k views

Painting with Line Filler

Given a matrix of positive integers, output whether it's possible to generate it by starting with an empty1 matrix of the same dimensions and repeatedly filling a whole row or a whole column with the ...
1 vote
0 answers
70 views

How to divide the columns in a time series by their first value in R? [closed]

I have the code below working. But there must be a better way. ...
8 votes
14 answers
781 views

Ragged Matrix Indexing

A ragged matrix, is a matrix that has a different number of elements in each row. Your challenge is to write a program in any favorable language to find the indices of all occurrences of target in the ...
16 votes
11 answers
802 views

Construct a Gaussian Matrix

Gaussian blur is a method used for blurring images smoothly. It involves creating a matrix which will be used by convolving it with the pixels of an image. In this challenge, your task is to construct ...
25 votes
19 answers
2k views

Double trace of a square matrix

Inspired by a question (now closed) at Stack Overflow. Given a square matrix, let its double trace be defined as the sum of the entries from its main diagonal and its anti-diagonal. These are marked ...
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} & {\...
14 votes
13 answers
1k views

Make a Custom Bayer Matrix

A Bayer matrix is a threshold map used for ordered dithering that gives the illusion of having more shades of color than actually present by using a crosshatch-like pattern. Bayer matrices are square ...
9 votes
5 answers
434 views

3D rotation matrix to quaternion

There are multiple ways to represent a 3D rotation. The most intuitive way is the rotation matrix – $$A=\begin{bmatrix}A_{11}&A_{12}&A_{13}\\A_{21}&A_{22}&A_{23}\\A_{31}&A_{32}&...
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 ...
31 votes
57 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: ...
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 &...
24 votes
16 answers
2k views

Reconstruct Matrix from its diagonals

Given the diagonals of a matrix, reconstruct the original matrix. The diagonals parallel to the major diagonal (the main diagonals) will be given. Diagonals: ...
13 votes
38 answers
2k views

Odd'em out: letters

Objective Write a program or function (or equivalent) that sorts out and returns the odd letter in the matrix of random size. Details You will be passed a matrix (as a string) as input of random ...
16 votes
12 answers
1k views

Is this a shift matrix?

A Shift matrix is a binary matrix with one superdiagonal or subdiagonal formed by only ones, everything else is a zero. A superdiagonal/subdiagonal is a diagonal parallel to the main diagonal, which ...
15 votes
16 answers
1k views

Minimum rotation to get the maximum value

I recently solved a coding challenge in one of the challenge papers that my IT teacher gave to us. It was a seemingly simple, but fun challenge, so I thought it will make fun golfing. The task Given ...
3 votes
0 answers
124 views

4D rotation matrix to quaternions

It is well-known that a 3D rotation can always be represented by a quaternion. It is less well-known that a 4D rotation can always be represented by two quaternions, sending a point \$p=(a,b,c,d)^T\$ ...
11 votes
9 answers
621 views

Find Sub-matrix with matched cell 1

You are given a matrix of size m x n where each cell can contain either 1 or 0. You need to find the largest square submatrix that contains only 1's. The output should be the area of the largest ...
7 votes
2 answers
473 views

NxM List Combination Closest to Target

Given a list of N lists, each containing M positive integers, and a separate list of M positive integers (target values), return a list of N scalars (integers with a value of 0 or more) that ...
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 ...
27 votes
47 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\$, ...
21 votes
11 answers
2k views

Is this a Jordan matrix?

Background A Jordan matrix is a block-diagonal matrix where each block on the diagonal has the structure of $$ \begin{bmatrix} \lambda & 1 & 0 & \cdots & 0 \\ 0 &...
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'...
25 votes
17 answers
3k views

Codegolf the Hafnian

The challenge is to write codegolf for the Hafnian of a matrix. The Hafnian of an \$2n \times 2n\$ symmetric matrix \$A\$ is defined as: $$\operatorname{haf}(A) = \frac 1 {n!2^n} \sum_{\sigma \in S_{...
29 votes
26 answers
1k 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^...
18 votes
4 answers
649 views

Calculate the integer square root of a matrix

Let \$A\$ be a square matrix that is at least \$2 \times 2\$ where each element is an integer. \$A^2 = A \times A\$ will then have the same dimensions as \$A\$, and will have integer elements. For ...
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 ...
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 ...
28 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 ...
20 votes
15 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 ...
3 votes
1 answer
180 views

Implement Strassen's algorithm [closed]

Strassen's algorithm was the first method of matrix multiplication in subcubic time complexity, namely O(n**log2(7)) for a pair of ...
20 votes
12 answers
2k views

Make all the squares explode

You are given a square matrix of width \$\ge2\$, containing square numbers \$\ge1\$. Your task is to make all square numbers 'explode' until all of them have disappeared. You must print or return the ...
29 votes
29 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 ...
9 votes
8 answers
380 views

Find the representative submatrix

Note: This is a more limited version of a challenge from 2014 that only received one answer. That challenge required participants write two programs; this challenge is essentially one half of that. ...
16 votes
10 answers
1k views

Find the box by its corners

Given a matrix of integers \$M\$ and a list of four integers \$L\$, find the sub-matrix \$N\$ whose corners are given by \$L\$ and return the sum of its elements. Suppose you're given the list \$L = [...
16 votes
10 answers
2k views

Make the cheapest cut

“Cut” a matrix of integers on the line where the sum of the absolute differences of “severed” neighbors is the least. Example Consider this matrix: ...
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. ...
46 votes
14 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 ...
24 votes
4 answers
940 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 ...
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 ...
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. ...
24 votes
18 answers
5k 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&...
13 votes
7 answers
781 views

Box blur the string

Box blur is a simple operation for blurring images. To apply box blur, simply replace each pixel with the average of its and the surrounding 8 pixels' values. Consider, for example, the following ...
14 votes
4 answers
497 views

Find a matrix whose sliding sum is the input

Given a matrix like this: 1 1 3 -2 3 -4 1 -1 1 1 1 0 0 -1 0 0 By taking a 2×2 "sliding sum", where the sum of every 2×2 region of the matrix is ...
10 votes
2 answers
3k views

Reduced Row-Echelon Form of a Matrix

The goal of this challenge is to create a program that takes in a matrix and outputs its reduced row-echelon form. A matrix is in reduced row-echelon form if it meets all of the following ...
26 votes
57 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 ...

1
2 3 4 5 6