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.
295
questions
9
votes
5
answers
798
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 ...
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, ~ ...
24
votes
38
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 \...
1
vote
0
answers
108
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 ...
1
vote
0
answers
110
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 ...
20
votes
23
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 ...
11
votes
6
answers
428
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 ...
1
vote
0
answers
78
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.
...
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 ...
4
votes
1
answer
188
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\$ ...
9
votes
5
answers
454
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}&...
11
votes
9
answers
640
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 ...
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 ...
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 ...
3
votes
1
answer
188
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 ...
9
votes
8
answers
401
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:
...
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 ...
13
votes
7
answers
793
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
505
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 ...
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: ...
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 ...
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 &...
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 ...
10
votes
15
answers
355
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 "...
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 ...
12
votes
10
answers
991
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} & \...
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 ...
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 ...
20
votes
12
answers
973
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. ...
12
votes
3
answers
291
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 ...
2
votes
5
answers
159
views
Generate this string table [duplicate]
For the given string s, generate generate a table like below.
...
7
votes
2
answers
476
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 ...
3
votes
3
answers
331
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 ...
2
votes
2
answers
207
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\...
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 ...
7
votes
2
answers
226
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 ...
11
votes
7
answers
735
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 ...
12
votes
5
answers
914
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 ...
22
votes
6
answers
799
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\$ ...
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 ...
14
votes
14
answers
802
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
...
8
votes
14
answers
791
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 ...
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 ...
8
votes
8
answers
454
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 ...
10
votes
7
answers
312
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.
...
13
votes
13
answers
2k
views
Mat Printing Matrix
A manufacturing company wants to print a design on mats of varying dimensions, and they hired you to program a robot to make these mats. The design consists of alternating rings of any 2 symbols on a ...
15
votes
4
answers
690
views
Simulate weathering of a rock
Inspired by my geography class, Sandboxed
Simulate weathering / erosion.
Input
Input will be given as a string of any 2 characters (or see below) you like in the shape of the rock (here i use spaces ...
19
votes
6
answers
786
views
Is it an elementary matrix?
Consider a linear system of equations, in \$n\$ unknowns, expressed as
$$A \textbf x = \textbf b$$
where \$A \in M_{n,n}(\mathbb Z)\$ is an \$n \times n\$ matrix of integers, \$\textbf x\$ is a column ...