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.

Filter by
Sorted by
Tagged with
19
votes
7answers
1k 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 ...
14
votes
21answers
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 ...
31
votes
21answers
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 ...
20
votes
20answers
1k views

Reorder a matrix, twice

You are given a square \$n \times n\$ matrix \$A\$, and a list (or vector) \$u\$ of length \$n\$ containing the numbers \$1\$ through \$n\$ (or \$0\$ through \$n-1\$). Your task is to reorder the ...
27
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 ...
22
votes
22answers
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"] ...
28
votes
23answers
4k views

Binary Numbers Magic Trick

The challenge is simply; output the following six 2D integer arrays: ...
9
votes
7answers
1k views

An array battle with odd secret powers

Here is a relatively simple two dimensional array challenge. Imagine a battlefield of 625 foot soldiers. You command the odd troops, but unfortunately the strength of the even troops overwhelms you. ...
9
votes
0answers
169 views

Decompose Commutators

A theorem in this paper1 states that every integral n-by-n matrix M over the integers with trace M = 0 is a commutator, that means there are two integral matrices A,B of the same size ...
17
votes
19answers
3k views

Hermitian matrix?

Note that this challenge requires no handling or understanding of complex numbers. Given a non-empty square matrix where every element is a two-element (Re,Im) integer list, determine (giving any ...
21
votes
6answers
976 views

Subtract the Folded Matrix

Challenge: Given an NxN matrix where \$N\geq2\$ and one of eight distinct 'folding options', output a 2D array/list with the subtracted values. The eight folding options are: left-to-right; right-to-...
85
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 ...
21
votes
7answers
987 views

Is this a submatrix?

This is the 2-dimensional generalisation of this challenge. For our purposes, one matrix (or 2D array) A is considered a submatrix of another matrix B, if A can be obtained by completely removing a ...
20
votes
11answers
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 ...
20
votes
21answers
2k views

How to print the below format in the fewest bytes?

This challenge is inspired by this, now deleted question. Take a positive integer N as input, and output a matrix with the numbers 1 .. N2 that follows the pattern below: Fill in the first row with ...
16
votes
8answers
655 views

Classify a region by its slope

Definitions The kth ring of a square matrix of size N, where 1 ≤ k ≤ ceiling(N/2) is the list formed by the elements of the kth and (N-k+1)th rows and columns, but without the first and last k-1 ...
21
votes
0answers
529 views

Matrices Generated Using Rectangles

Input is an r by c matrix of non-negative integers (you may input r and c as other parameters if this helps with golf). This may be inputted as a 2D array or, if you prefer, a 1D array (though taking ...
21
votes
12answers
1k views

Slither Like a Snake

The Idea We've done matrix spirals before, and full rotations, and even diagonal rotations, but not, as far as I can find, snake rotations! What is a snake rotation? Imagine the rows of a matrix ...
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 ...
12
votes
3answers
400 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 ...
10
votes
15answers
332 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 ...
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 ...
7
votes
6answers
554 views

Black Hole challenge from codewars

I came across this challenge from codewars. I wish to know how to solve this problem. The problem description is given below. Its elements are in range[1..n^2]. The matrix is filled by rings, from ...
15
votes
15answers
2k 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 ...
19
votes
7answers
765 views

Hilbert-Curvify a Matrix

Inspired by this question Another way to unroll a 2D image into a 1D string is to use an Hilbert Curve. There are many version of this curve, depending on the number of iterations used while ...
8
votes
2answers
336 views

Can this container hold this much liquid?

Can this container hold this much liquid? Challenge Synopsis As you most likely know, liquids have an indefinite shape and a definite volume. As such, they always take the shape of their container. ...
13
votes
6answers
756 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 ...
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)\$...
10
votes
4answers
411 views

Cell phone Charge

Challenge Taken with permission from my University Code Challenge Contest The dependence we have on mobile phones makes us charge them every night up to the maximum level of the battery, so we do not ...
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 ...
15
votes
4answers
568 views

Rotate every row and column in a matrix

The Challenge Given a n x n matrix of integers with n >= 2 1 2 3 4 and a list of integers with exactly ...
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 <...
16
votes
7answers
2k views

Borderless table

In this challenge you are going to place letters from the alphabet in a Cartesian plane and output the result as a text. Your input will consist in a list of list with 3 parameters: X coordinate Y ...
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 ...
17
votes
14answers
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: ...
18
votes
2answers
423 views

Is magic possible?

A magic square is an n-by-n square grid, filled with distinct positive integers in the range 1,2,...n^2, such that each cell contains a different integer and the sum of the integers in each row, ...
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....
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 ...
13
votes
20answers
1k views

Compute the Optimal Square Matrix

The optimal matrix (for the rather narrow scope of this challenge) is obtained by "zipping" the elements from the corresponding rows and columns of a square matrix and getting the maximum of each pair....
12
votes
13answers
489 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 ...
16
votes
18answers
774 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 ...
6
votes
5answers
193 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: ...
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 ...
10
votes
9answers
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 ...
10
votes
13answers
368 views

Dot Product of Diagonals

This challenge is very simple. You are given as input a square matrix, represented in any sane way, and you have to output the dot product of the diagonals of the matrix. The diagonals in specific ...
13
votes
9answers
1k views

Characteristic polynomial

The characteristic polynomial of a square matrix A is defined as the polynomial pA(x) = det(Ix-A) where I is the identity matrix and det the determinant. Note that this definition always gives us a ...
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 ...
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 ...
20
votes
15answers
2k views

Find the Cell's Neighbours

...or Toroidal Moore Neighbourhoods Given positive integers h, w and a non-negative integer ...
21
votes
14answers
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 contain \$k\...