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
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 &...
22
votes
28
answers
1k
views
Matrix with 1 to L(n), in all n columns
Challenge:
Take a list, L containing positive integers as input:
3 5 2 1 6
and create a matrix where the n'th column contains the vector 1:L(n), where shorter ...
31
votes
55
answers
2k
views
Generate this number table
Given an integer \$1 < n < 10 \$ generate a table like below.
For \$n = 5\$,
1 2 3 4 5
2 2 3 4 5
3 3 3 4 5
4 4 4 4 5
5 5 5 5 5
For \$n = 8\$,
...
22
votes
21
answers
1k
views
Find the submatrix with the smallest mean
You're given a n-by-m matrix of integers, where n,m > 3. Your task is to find the 3-by-3 sub-matrix that has the lowest mean, and output this value.
Rules and clarifications:
The integers will be ...
16
votes
20
answers
1k
views
Sorting rectangular matrices
Given a matrix A, we can say that it is "sorted" if each row and column of A is sorted (ascending left-to-right for ...
14
votes
15
answers
690
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
votes
35
answers
2k
views
Index of the row with most non-zero elements
This is a simple one: Take a matrix of integers as input, and output the index of the row with the most non-zero elements. You may assume that there will only be one row with the most non-zero ...
32
votes
34
answers
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 ...
27
votes
58
answers
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 ...
32
votes
59
answers
3k
views
Create a checkerboard matrix
Take a positive integer n as input, and output a n-by-n checkerboard matrix consisting of 1 and 0.
The top left digit should always be 1.
Test cases:
...
26
votes
19
answers
3k
views
Is it a stochastic matrix?
A stochastic matrix is a matrix of probabilities used in the context of Markov chains.
A right stochastic matrix is a matrix where each row sums to 1.
A left ...
15
votes
16
answers
7k
views
Generate all square sub-matrices of a given size
You will be given a square matrix of integers M and another positive integer n, strictly smaller than the size of M. Your task is to generate all square sub-matrices of M of size n.
For the purposes ...
16
votes
20
answers
1k
views
Turn this array into a matrix
Take a non-nested array as input. Turn it into a matrix by using the following method:
Let's say my array is [1, 2, 3, 4, 5]
First, I repeat that array 5 times: (...
48
votes
106
answers
6k
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 <...
21
votes
17
answers
3k
views
Check if all non-zero elements in a matrix are connected
Input:
A matrix containing integers in the range [0 - 9].
Challenge:
Determine if all non-zero elements are connected to each other vertically and/or horizontally.
Output:
A truthy value if all ...
19
votes
29
answers
3k
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 ...
19
votes
8
answers
1k
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 ...
27
votes
10
answers
4k
views
Symbolic matrix multiplication
There are lots of different ways to explain matrix multiplication. I'll stick with a single figure since I believe most people here are familiar with it (and the figure is very descriptive). If you ...
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 \...
22
votes
14
answers
2k
views
Is the matrix rank-one?
Given a matrix of integers, test if it's rank-one, meaning that every row is a multiple of the same vector. For example, in
...
10
votes
14
answers
749
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 are ...
48
votes
16
answers
6k
views
Zigzagify a Matrix
As part of its compression algorithm, the JPEG standard unrolls a matrix into a vector along antidiagonals of alternating direction:
Your task is to take a matrix (not necessarily square) and return ...
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\$ ...
15
votes
9
answers
4k
views
Matrix Trigonometry
Introduction
The two most common trigonometric functions, sine and cosine (or sin and ...
14
votes
13
answers
2k
views
Characteristic polynomial
The characteristic polynomial of a square matrix \$A\$ is defined as the polynomial \$p_A(x) = \det(Ix-A)\$ where \$I\$ is the identity matrix and \$\det\$ the determinant. Note that this definition ...
14
votes
39
answers
2k
views
Odd'em out: letters
Objective
Write a program or function (or equivalent) that sorts out and returns the odd letter in the matrix of random size.
Details
You will be passed a matrix (as a string) as input of random ...
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, ~ ...
9
votes
5
answers
796
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 ...
11
votes
6
answers
426
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 ...
29
votes
31
answers
3k
views
Antisymmetry of a Matrix
A matrix is antisymmetric, or skew-symmetric, if its transpose equals its negative.
The transpose of a matrix can be obtained by reflecting its elements across the main diagonal. Examples of ...
12
votes
2
answers
358
views
Positioning a fake mirror for best effect
Challenge
Premise
Consider a mosaic of \$m\times n\$ tiles, in \$k\$ unique colours designated by integers. Example (\$3\times6\$, four colours):
...
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 ...
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 ...
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 ...
22
votes
3
answers
840
views
Find the number of n-by-n (-1, 0, 1) matrices with zero permanent as quickly as possible
The permanent of an \$n\$-by-\$n\$ matrix \$A = (a_{i,j})\$ is defined as:
$$\operatorname{perm}(A)=\sum_{\sigma\in S_n}\prod_{i=1}^n a_{i,\sigma(i)}$$
For a fixed \$n\$, consider the \$n\$-by-\$n\$ ...
37
votes
39
answers
4k
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 ...
15
votes
9
answers
444
views
Compute the perimeter density matrix
Introduction
The perimeter density matrix is an infinite binary matrix M defined as follows.
Consider a (1-based) index (x, y), and denote by M[x, y] the rectangular sub-matrix spanned by the corner (...
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 ...
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.
...
8
votes
14
answers
790
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 ...
16
votes
11
answers
806
views
Construct a Gaussian Matrix
Gaussian blur is a method used for blurring images smoothly. It involves creating a matrix which will be used by convolving it with the pixels of an image. In this challenge, your task is to construct ...
25
votes
19
answers
2k
views
Double trace of a square matrix
Inspired by a question (now closed) at Stack Overflow.
Given a square matrix, let its double trace be defined as the sum of the entries from its main diagonal and its anti-diagonal. These are marked ...
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} & {\...
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 ...
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}&...
27
votes
21
answers
4k
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 ...
27
votes
45
answers
3k
views
Output a Latin Square
An \$n\times n\$ Latin Square is a grid containing exactly \$n\$ distinct values where the values in each row and column are distinct. For example,
$$\begin{matrix}
A & B & C \\
C & A &...
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
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 ...
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 ...