Skip to main content

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
2k views

Is this a Hadamard matrix?

Hadamard matrices is a square matrix whose entries are either +1 or −1 and whose rows are mutually orthogonal. In other words, it means that each pair of rows has matching entries in exactly half of ...
Fmbalbuena's user avatar
  • 4,225
11 votes
5 answers
1k views

9-16-25 2D Matrix

Given three grids and the sum of rows and columns of each grid, your task is: Grid \$3×3\$: Fill the grid from \$1\$ to \$9\$, ensure no repeats within the grid. Grid \$4×4\$: Fill the grid from \$1\...
user avatar
12 votes
6 answers
1k views

Contract a tensor

Introduction Tensor contraction is an operation that can be performed on a tensor. It is a generalization of the idea of the trace of a matrix. For example, if we have a rank-2 tensor (a matrix) and ...
Tbw's user avatar
  • 2,123
4 votes
1 answer
610 views

Which line is best?

Consider an \$n \times n\$ grid of integers. The task is to draw a straight line across the grid so that the part that includes the top left corner sums to the largest number possible. Here is a ...
Simd's user avatar
  • 3,153
9 votes
5 answers
960 views

Compute the logarithm of a matrix

There have already been challenges about computing the exponential of a matrix , as well as computing the natural logarithm of a number. This challenge is about finding the (natural) logarithm of ...
bsoelch's user avatar
  • 6,035
17 votes
22 answers
2k views

Compute this fractal matrix

The unique-disjointness matrix ( UDISJ(n) ) is a matrix on all pairs of subsets of {1...,n} with entries $$ U_{(A,B)}=\begin{cases} 0, ~ if ~ |A\cap B|=1\\ 1, ~ ...
bsoelch's user avatar
  • 6,035
24 votes
40 answers
2k views

Diagonalize a vector

Diagonalize a vector into a matrix. Input A vector, list, array, etc. of integers \$\mathbf{v}\$ of length \$n\$. Output A \$n \times n\$ matrix, 2D array, etc. \$A\$ such that for each element \$a_i \...
bigyihsuan's user avatar
  • 10.4k
1 vote
0 answers
119 views

Bunny Shakespeare poetry [closed]

Suppose you have a grid of cells that is 6 wide and arbitrarily tall. In each cell is a word. Let the bottom row, which is row #1, be the last 6 words of sample.txt in order. Let the second to last ...
mikebolt's user avatar
  • 121
1 vote
0 answers
119 views

Print all semimagic squares [closed]

I am working on a code to print all semimagic squares [1] of a given size. I am working with the following definition: An \$n\times n\$ consists of numbers \$1,2,\cdots, n^2\$. All numbers must be ...
ananta's user avatar
  • 111
23 votes
29 answers
2k 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 ...
Kevin Cruijssen's user avatar
11 votes
6 answers
774 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 ...
Daniel Schepler's user avatar
14 votes
13 answers
2k 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 ...
Yousername's user avatar
  • 4,060
4 votes
1 answer
211 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\$ ...
Parcly Taxel's user avatar
  • 3,935
9 votes
5 answers
481 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}&...
Parcly Taxel's user avatar
  • 3,935
11 votes
9 answers
660 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 ...
user avatar
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 ...
l4m2's user avatar
  • 26k
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 ...
math scat's user avatar
  • 9,438
3 votes
1 answer
196 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 ...
DroneBetter's user avatar
9 votes
8 answers
437 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. ...
Jordan's user avatar
  • 10.5k
17 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 = [...
Jordan's user avatar
  • 10.5k
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: ...
Jordan's user avatar
  • 10.5k
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 ...
oeuf's user avatar
  • 1,833
14 votes
8 answers
833 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 ...
Jordan's user avatar
  • 10.5k
14 votes
4 answers
512 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 ...
rydwolf's user avatar
  • 18.9k
24 votes
17 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: ...
math scat's user avatar
  • 9,438
16 votes
11 answers
2k views

Flood fill by distance

Say I have a grid with some obstacles: ---- | X | | | |XX | | | ---- I can fill this with numbers, as distance from the top left corner. But, because ...
emanresu A's user avatar
  • 41.6k
27 votes
19 answers
3k views

Create Bernard from Desmos

Background In the online graphing calculator Desmos, there is a certain shape that appears in the lower left portion of the graph in many high detail graphs, which the Desmos community has dubbed &...
Aiden Chow's user avatar
6 votes
1 answer
2k views

fastest matrix multiplication on x86

This challenge requires integration with C, so you can stop reading if you're not interested. Matrix multiplication is a simple operation, but the performance depends a lot on how efficiently the code ...
xiver77's user avatar
  • 2,375
10 votes
15 answers
360 views

center a matrix [closed]

The words "center a" probably give flashbacks to any decent HTML developer. Luckily, we are not working with divs, we are working with matrices. Given a matrix where w=h, and given a "...
Chspsa's user avatar
  • 691
14 votes
10 answers
2k views

Storing a band matrix

Related challenge 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 ...
DLosc's user avatar
  • 38.8k
13 votes
10 answers
1k views

Convert J brackets back to 2-D list

Let's say we have a 2-D list, like the following one: \$ \begin{bmatrix} \color{red}{1} & \color{red}{2} & \color{red}{3} & \color{red}{4} \\ \color{blue}{5} & \color{blue}{6} & \...
naffetS's user avatar
  • 12.8k
13 votes
12 answers
3k views

Simulate the ripple effect

Imagine throwing a rock into a pond. You get to see perfect circles of ripples spreading out over the pond, bouncing off of each other. Of course, nothing can be as serene as that in coding, but, we ...
Chspsa's user avatar
  • 691
40 votes
13 answers
5k views

Recreate Minecraft's lighting

Minecraft has a fairly unique lighting system. Each block's light value is either one less than the brightest one surrounding it, or it is a light source itself. Your task is to write a method that ...
Chspsa's user avatar
  • 691
20 votes
12 answers
984 views

Convert between graph representations

What? Let's say I have this graph: 1 \ \ 2 3 \ / \ / 4 I can represent it in 2 ways: A list of connected vertices. ...
zoomlogo's user avatar
  • 1,655
12 votes
3 answers
298 views

Line Islands in a Word Search

My third word search related challenge in a row. :) Challenge: Brief explanation of what a word search is: In a word search you'll be given a grid of letters and a list of words. The idea is to cross ...
Kevin Cruijssen's user avatar
2 votes
5 answers
164 views

Generate this string table [duplicate]

For the given string s, generate generate a table like below. ...
sinvec's user avatar
  • 1,993
7 votes
2 answers
486 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 ...
drmosley's user avatar
  • 757
3 votes
3 answers
335 views

Appease the Picky Eater

Your friend Jack is a picky eater. He only likes certain foods, and he only likes to eat a certain amount of them each day. Despite this, Jack has very strict calorie and macronutrient goals that he ...
drmosley's user avatar
  • 757
3 votes
2 answers
218 views

Resize the image

Given an \$n\times m\$ matrix \$A\$ and two integers \$w,h\$, output a matrix of \$w\times h\$ called \$B\$, such that $$B_{i,j} = \int_{i-1}^i\mathbb dx\int_{j-1}^j A_{\left\lceil \frac xw\cdot n\...
l4m2's user avatar
  • 26k
20 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 ...
DLosc's user avatar
  • 38.8k
8 votes
2 answers
230 views

Count the ways to transform (2)

Your input is a matrix (2d array) of positive integers. For example: \begin{matrix} 1 & 1 & 4 & 7\\ 1 & 3 & 5 & 6\\ 2 & 1 & 4 & 5 \\ \end{matrix} A semi-continuous ...
AnttiP's user avatar
  • 7,918
12 votes
7 answers
749 views

Knights Jam | Chess

The knight is a chess piece that, when placed on the o-marked square, can move to any of the x-marked squares (as long as they ...
DialFrost's user avatar
  • 5,097
12 votes
5 answers
923 views

Simulating Bombs

More advanced version of this The aim of this puzzle is to simulate the impact of the shock wave of bombs on a square grid. The grid initially contains cells labelled ...
DialFrost's user avatar
  • 5,097
22 votes
6 answers
812 views

Check if a matrix is Zarankiewicz-maximal

My final-year project at the National University of Singapore is on Zarankiewicz's problem: What is the maximum number of 1s in an \$m×n\$ binary matrix (\$m\$ rows and \$n\$ columns) with no \$a×b\$ ...
Parcly Taxel's user avatar
  • 3,935
21 votes
13 answers
2k views

Output the 12 Bar Blues

Note to those without experience in music: Through making an attempt to solve this problem, you may find that music and computer programming are similar in the ways that they implement rules and ...
drmosley's user avatar
  • 757
14 votes
14 answers
808 views

SuperSquares NxN

Given an integer N from 1-9, you must print an NxN grid of NxN boxes that print alternating 1s and Ns, with each box having an alternating starting integer. Examples ...
drmosley's user avatar
  • 757
8 votes
14 answers
813 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 ...
DialFrost's user avatar
  • 5,097
23 votes
18 answers
2k views

Print a NxN integer involute

Given an integer \$N\$, you must print a \$N\times N\$ integer involute with the numbers increasing in a clockwise rotation. You can start with either 0 or 1 at the top left, increasing as you move ...
Cool guy's user avatar
  • 431
9 votes
8 answers
456 views

Count the ways to transform

A rigid transformation of a square array is a mapping from square arrays of a certain size to square arrays of the same size, which rearranges the elements of the array such that the distance to each ...
Wheat Wizard's user avatar
  • 99.6k
10 votes
7 answers
318 views

Cell Evolution Matrix

Your lab needed to simulate how a particular cell evolves over time in a 2D grid space. A sample 2D grid space below shows a single cell at the centre of the grid. ...
DialFrost's user avatar
  • 5,097

1
2 3 4 5 6