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
13 votes
17 answers
13k views

Euclidean Distance Between Two Matrices

Your task is to write the shortest algorithm in a language of your choosing that accomplishes the following: Given two matrices it must return the euclidean distance matrix. The euclidean distance ...
caseyk's user avatar
  • 337
25 votes
14 answers
3k views

ASCII texturing

Write a function or a full program that applies an ASCII texture to a 2d shape. Inputs: shape, texture, dimensions(optionally). Both Shape and Texture can be given by any convenient method (2d ...
AZTECCO's user avatar
  • 10.7k
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 ...
hyper-neutrino's user avatar
  • 41.4k
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: ...
hyper-neutrino's user avatar
  • 41.4k
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 ...
ihavenoidea's user avatar
  • 1,219
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 ...
ngn's user avatar
  • 15.2k
2 votes
0 answers
87 views

Generate square sub-matrices of 1's [duplicate]

Back again, stealing another question from stackoverflow. You are given a list of integers >= 0. Generate in order square 1-matrices where the side length is given by the list entry of the current ...
jaaq's user avatar
  • 617
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 ...
HiddenBabel's user avatar
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 ...
Adám's user avatar
  • 30k
21 votes
20 answers
2k 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 ...
Sanchises's user avatar
  • 9,466
16 votes
1 answer
399 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 as M such that M ...
flawr's user avatar
  • 43.8k
12 votes
7 answers
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. ...
WarmTaunTaun's user avatar
22 votes
6 answers
1k 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-...
Kevin Cruijssen's user avatar
23 votes
22 answers
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"] ...
Kevin Cruijssen's user avatar
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 ...
Arnauld's user avatar
  • 191k
29 votes
23 answers
6k views

Binary Numbers Magic Trick

The challenge is simply; output the following six 2D integer arrays: ...
Kevin Cruijssen's user avatar
25 votes
12 answers
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 ...
Jonah's user avatar
  • 33.3k
25 votes
11 answers
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 ...
Laikoni's user avatar
  • 26.1k
7 votes
6 answers
618 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 the ...
James K J's user avatar
  • 173
8 votes
2 answers
374 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. ...
GMills's user avatar
  • 946
25 votes
8 answers
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)\$...
Luis felipe De jesus Munoz's user avatar
10 votes
4 answers
466 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 ...
Luis felipe De jesus Munoz's user avatar
16 votes
7 answers
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 ...
Luis felipe De jesus Munoz's user avatar
19 votes
15 answers
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: ...
Kevin Cruijssen's user avatar
14 votes
6 answers
822 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 ...
Adám's user avatar
  • 30k
14 votes
3 answers
536 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 ...
Luis felipe De jesus Munoz's user avatar
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....
Adám's user avatar
  • 30k
25 votes
15 answers
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 ...
Kevin Cruijssen's user avatar
10 votes
15 answers
417 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 ...
AdmBorkBork's user avatar
  • 43.3k
19 votes
21 answers
922 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 ...
Adám's user avatar
  • 30k
14 votes
15 answers
696 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 ...
Adám's user avatar
  • 30k
7 votes
5 answers
208 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: ...
Arcturus's user avatar
  • 7,347
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 ...
Arnauld's user avatar
  • 191k
13 votes
16 answers
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 ...
Mr. Xcoder's user avatar
  • 42.4k
15 votes
15 answers
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 ...
Luis felipe De jesus Munoz's user avatar
19 votes
12 answers
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\$ ...
SEJPM's user avatar
  • 3,393
7 votes
4 answers
690 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 ...
Andreï V. Kostyrka's user avatar
10 votes
7 answers
713 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 ...
Mr. Xcoder's user avatar
  • 42.4k
21 votes
14 answers
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 ...
Arnauld's user avatar
  • 191k
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. ...
DimChtz's user avatar
  • 957
21 votes
11 answers
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 ...
Stewie Griffin's user avatar
11 votes
5 answers
374 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 ...
Conor O'Brien's user avatar
15 votes
16 answers
5k views

Is the bus load legal?

I was in the bus today, and noticed this sign: ...
Adám's user avatar
  • 30k
26 votes
16 answers
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 ...
Luis felipe De jesus Munoz's user avatar
32 votes
20 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 ...
ibrahim mahrir's user avatar
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} & {\...
alephalpha's user avatar
  • 47.5k
19 votes
21 answers
2k views

Collapsing Matrices

Related: Let's design a digit mosaic, Print/Output the L-phabet. Sandbox post here Given 2 inputs C = columns and rows, S = starting point output a matrix as ...
Luis felipe De jesus Munoz's user avatar
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&...
Beta Decay's user avatar
  • 23.3k
21 votes
16 answers
2k views

Find the Cell's Neighbours

...or Toroidal Moore Neighbourhoods Given positive integers h, w and a non-negative integer ...
Dom Hastings's user avatar
  • 23.2k
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] \...
Mr. Xcoder's user avatar
  • 42.4k