# 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.

274 questions
Filter by
Sorted by
Tagged with
1k views

### Spot all (anti)diagonals with duplicated values

Challenge: Given a matrix input, determine the amount of diagonals and anti-diagonals with duplicated numbers. So if we have a matrix like this: ...
• 108k
805 views

### Binary self-rotation

Given a binary 3D array, for each layer, cyclically rotate up each of its columns as many steps as indicated by the binary encoding of the columns of the layer above it, and then cyclically rotate ...
• 28.3k
512 views

### Matrix rotation sort

Lets define a non-empty, unsorted and finite matrix with unique numbers as follow: $$N = \begin{Bmatrix} 4&5&7\\1&3&6 \end{Bmatrix}$$ Lets define 4 matrix moves as: ↑* (up): Moves a ...
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....
• 28.3k
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 ...
• 108k
397 views

### Matrix Jigsaw Puzzle Pieces

(Randomly inspired by https://codegolf.meta.stackexchange.com/a/17272/42963) Given a rectangular matrix of digits (i.e., 0 - 9), output the "pieces" of the matrix ...
• 42.9k
911 views

### Most contributing rows

Given a non-empty matrix of non-negative integers, answer which unique rows contribute most to the sum total of elements in the matrix. Answer by any reasonable indication, for example a mask of the ...
• 28.3k
629 views

### Count the contiguous submatrices

Migrated from chat Given two non-empty non-negative integer matrices A and B, answer the number of times A occurs as a contiguous, possibly overlapping, submatrix in B. Examples/Rules 0. There may not ...
• 28.3k
206 views

### Find equally-weighted complete graphs

Graph theory is used to study the relations between objects. A graph is composed of vertices and edges in a diagram such as this: ...
• 7,259
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
2k views

### Trim that distracting background off!

Isn't it annoying when you're taking a picture, but the background detracts from the actual substance of the image? I'd say it is. I need to know how much I should crop so that I get rid of this ...
• 41.6k
3k views

### Output the anti-clockwise inward spiral of a 2D array

From this stackoverflow question Given a 2D array of size $M \times N$, output the values in a anti-clockwise fashion. The output must start from the outside to the inside and the initial point ...
2k views

### Is the Matrix Positive-Definite?

Introduction Today we're gonna take care of the bane of first-year linear algebra students: matrix definiteness! Apparently this doesn't yet have a challenge so here we go: Input A $n\times n$ ...
• 3,283
642 views

### Ordinary Least Squares Regression

I am quite surprised that a variant of linear regression has been proposed for a challenge, whereas an estimation via ordinary least squares regression has not, despite the fact the this is arguably ...
• 1,430
702 views

### Whose neighbours are hostile?

Introduction For the purposes of this challenge, we will define the neighbours of an element $E$ in a square matrix $A$ (such that $E=A_{i,j}$) as all the entries of $A$ that are immediately ...
• 41.6k
2k views

### Chop off the matrix to get the desired sum

Definition Given a matrix $M$ of non-negative integers and a non-negative integer $k$, we define $F_k$ as the "chop-off" function that removes all rows and all columns in $M$ that ...
• 171k
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
2k views

### A knight's graph on an N-by-N board

In chess, a knight can only move to the positions marked with X relative to its current position, marked with ♞: A Knight's Graph is a graph that represents all legal moves of the knight chess piece ...
• 45.1k
363 views

### Minimal Centrosymmetrization

Topically related. Objective: Given a matrix of positive integers $M$, output the smallest centrosymmetric matrix which contains $M$ (this matrix may contain non-positive integers as well). A ...
• 39.1k
5k views

### Is the bus load legal?

I was in the bus today, and noticed this sign: ...
• 28.3k
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 ...
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 ...
4k views

• 22.8k
2k views

### Find the Cell's Neighbours

...or Toroidal Moore Neighbourhoods Given positive integers h, w and a non-negative integer ...
• 22.6k
4k views

• 16.5k
343 views

### Are there mountain rings?

Challenge Given a matrix of positive integers, determine if there are any "rings" of mountains. The formal definition for this challenge is: given a matrix of positive integers, is there any positive ...
• 40.3k
774 views

### Progression of Matrix Columns

Consider the infinite matrix: ...
• 42.9k
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
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 ...
• 41.6k
871 views

### The biggest square

This question is similar to Biggest Square in a grid. Challenge Given a matrix of 1 and 0 in a string format ...
2k views

### Getting the Vowel Square

Challenge You will create a function which takes a matrix filled with letters from the alphabet and determine if a 2x2 square composed of vowels exists. If a 2x2 ...
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 ...
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
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 ...
417 views

### The Lonely Islands

Input: A 2D array containing two distinct (optional) values. I'll use 0 and 1 when explaining the rules. The input format is of course flexible. Challenge: Zeros are water, and ones are islands. In ...
• 45.1k
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 ...
• 93.8k
331 views

### Matrix trace for any matrix through... Bresenham’s line rasterisation

Inspired by this. Agatha Stephendale, a sophomore who is really into raster graphics, has taken a course in linear algebra. Now she imagines matrices as rectangles, but in her artistic mind, she ...
• 1,430
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-...
• 108k
1k views

### Convergence of a Markov Process

Challenge Given a left- or right-stochastic matrix where the limit as x approaches infinity of the matrix to the power of x approaches a matrix with all finite values, return the matrix to which the ...
• 40.3k