The Stack Overflow podcast is back! Listen to an interview with our new CEO.

Questions tagged [matrix]

In mathematics, a matrix (plural matrices) is a rectangular array of numbers, symbols, or expressions, arranged in rows and columns. The individual items in a matrix are called its elements or entries.

Filter by
Sorted by
Tagged with
12
votes
3answers
736 views

Python algorithm that converts array-like data into MathJax

I frequently use MathJax, and often I need to write matrices. Since writing matrices using MathJax is very tiresome task, I decided to automate it. Code: ...
5
votes
2answers
186 views

Magic Square (Python)

Problem Write a method to return a boolean if an input grid is magic square. A magic square is a \$NxN\$ square grid (where N is the number of cells on each side) filled with distinct positive ...
3
votes
1answer
131 views

overlapping rectangles c#

I had to write code for a competition which calculates sum of the perimeter of three overlapping rectangles in a 2D space. rectangle A and rectangles B and rectangle c is overlapping each other.They ...
19
votes
4answers
2k views

Matrix class in C#

I've been learning C# during my free time in the past months; before that, I was mostly writing Java, so the transition hasn't been too hard, but I've never had my code reviewed or read by someone ...
2
votes
0answers
47 views

Compute angular distances between data points in a matrix

I am having a hard time speeding up a function that I use to compute angular distances between data points in a matrix. More precisely, I want to compute $$\sum_{r=1}^n|\pi - \arccos\left(\frac{(X_{i}-...
1
vote
0answers
72 views

Fast matrix transpose

I would like to improve the naive matrix transpose algorithm by using a cache friendly algorithm. So far there are 4 variants. Iterate over the source matrix: ...
1
vote
0answers
36 views

Stochastic gradient descent - backpropagation over mini-batches in one go

I have been working through a tutorial on neural networks. I have then been translating the code to java, to help improve my understanding of what the code is doing. The book mentions a way of ...
6
votes
1answer
10k views

Rotate Matrix using Python

This is my solution to Exercise 1.7 from Cracking the Coding Interview. I would appreciate any feedback on coding style and algorithm efficiency. I do know that there is an existing function in ...
3
votes
2answers
83 views

Large matrix with lots of repetitive fields (C++)

I wrote a timetabling program and I have been using a matrix to check for clashes between courses. Index (i, j) in the matrix ...
6
votes
2answers
106 views

Picking seats in a matrix

I'm trying to write a Python class that creates a matrix of zeros, then uses a random number generator to pick 'seats' on the matrix. It changes the zero in that seat to a one, until the matrix is all ...
1
vote
1answer
61 views

Extract numerics from matrix A with logical matrix B

I have a matrix A containing numeric values and a matrix B containing 0/1s: ...
2
votes
2answers
144 views

Ruby implementation for Hacker Rank connected cells in a grid

https://www.hackerrank.com/challenges/connected-cell-in-a-grid/problem problem statement: Consider a matrix where each cell contains either a 1 or a 0. Any cell containing a 1 is called a ...
4
votes
0answers
60 views

4×4 cofactor in SSE

The cofactor of a 4×4 matrix can be used to convert a "regular geometry" matrix into the matrix that transforms the normals. It's an alternative to the common inverse-transpose pattern. In this post I ...
5
votes
0answers
182 views

Creation of matrix with specific attempts at the end

What I want to do is build a Slay-the-Spire-like Map. As you can see in the following image, the map is splitted in ISteps and ...
4
votes
1answer
76 views

Find the number of pawns available for a rook to capture

I find that very very often with matrix questions, the traversal and comparison part of the solution is very verbose and repetitive. For example, this code (written for a coding challenge) to find ...
2
votes
2answers
86 views

Generate a large block diagonal covariance matrix with exponential decay

I am implementing Kalman filtering in R. Part of the problem involves generating a really huge error covariance block-diagonal matrix (dim: 18000 rows x 18000 columns = 324,000,000 entries). We denote ...
4
votes
1answer
69 views

Golang matrix package

I developed a small package to do matrix math. I just learned Go so I may have done it wrong. ...
2
votes
3answers
132 views

Modeling, view and projection transformation using vector and point in homogenous form

I know this is horrible code. This program is for performing modeling, view and projection transformation. The program reads input from scene.txt and outputs the <...
4
votes
1answer
131 views

Getting saddle points of a matrix

My homework assignment: Write a function called saddle that finds saddle points in the input matrix M. For the purposes of this problem, a saddle point is defined as an element whose value is ...
7
votes
2answers
3k views

Searching in a sorted 2D matrix

If I was at an interview, and wrote this code, what would you think? Please be brutal. Time it took to wrote it: 13 minutes Problem: Write an efficient algorithm that searches for a value in an m ...
5
votes
1answer
88 views

Pad a ragged multidimensional array to rectangular shape

For some machine learning purpose, I need to work with sequences with different lengths. To be able to process efficiently those sequences, I need to process them in batches of size ...
8
votes
2answers
2k views

Converting Data in Rows and Columns to Rows in VBA for Excel

I have a working VBA script for Excel that converts a matrix of data with multiple records in per row to multiple rows with one record per row. A StackOverflow user told me that the code could use ...
2
votes
0answers
35 views

Spiral printing elements of a matrix

As I'm learning elixir, I wrote some code for printing elements in the spiral of a matrix(2D array). I was wondering if there is any room for improvements with regards to its performance or ...
1
vote
1answer
111 views

Rotate matrix clockWise and counter clockWise

This code is a mix of the same question one time rotating to the right and one time to the left. I tried to simplify the code since I'm struggling with all of the indexing here. https://leetcode.com/...
4
votes
1answer
53 views

CUDA matrix class

I started to work on this CUDA C matrix class to learn both the object oriented programming in C++ and to learn CUDA. The initial goal of this project was to make a matrix class that can have almost ...
4
votes
1answer
66 views

Generating random sparse matrix

My goal is to generate a large sparse matrix with majority (~99%) zeros and ones. Ideally, I would be working with 10,000 rows and 10,000,000 columns. Additionally, each column is generated as a ...
3
votes
1answer
55 views

LeetCode: Transpose Matrix C#

Please review performance https://leetcode.com/problems/transpose-matrix/ Given a matrix A, return the transpose of A. The transpose of a matrix is the matrix flipped over it's main ...
3
votes
0answers
55 views

Striping a matrix concentrically, anticlockwise

Given a matrix you shall stripe the matrix, such that every number that has equal distance to the matrix border will be filled into an array. For examples see code comments. ...
3
votes
0answers
125 views

Python program to solve matrix-chain multiplication

An assignment at school required me to write a Python program for this task: In the matrix-chain multiplication problem, we are given a sequence of matrices ...
3
votes
3answers
55 views

Matrix input in N*N form in Python

I am using the below code in Python to input a N*N matrix in its original form: ...
5
votes
1answer
55 views

Multiplying a Matrix with its Transpose

Currently, I am in the process of optimizing a MIPS assembly program that takes a n x n matrix and multiplies it with its transpose. I am trying to optimize my matrix calculation algorithm so that it ...
4
votes
3answers
6k views

Rotate an N × N matrix 90 degrees clockwise

Given the problem from "Cracking the coding interview" Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees ...
2
votes
1answer
34 views

Transposing a square matrix (n x n)

I wish to transpose a square matrix, permanently overwriting it. This is not the same as creating a 2nd matrix with the transposed contents of the 1st matrix. I call the procedure with 3 parameters: ...
4
votes
2answers
4k views

4x4 matrix implementation in C++

I've been doing some 3D graphics in OpenGL lately and I needed a way to work with 4x4 matrices. My implementation supports the following operations: Matrix-matrix addition. Matrix-matrix subtraction. ...
3
votes
1answer
41 views

A matrix-vector multiply parallelized using boost::MPI

This program performs a matrix-vector multiply using MPI to split the computation up. It is not extremely robust (for example, it doesn't handle the case where the number of MPI processes does not ...
2
votes
1answer
126 views

GeeksforGeeks: Rotate matrix by 90 degrees

Please review the code readability, I would like to have an easier to follow code. Thanks ...
5
votes
1answer
81 views

3-function program computing connected components of a point cloud given a distance matrix

I wrote this code in Haskell (instead of Python) for the educational benefit. Can anyone suggest ways to improve this code? I'm guessing that I'm using fromIntegral...
3
votes
1answer
62 views

Sums of columns and diagonals of a matrix

I made a simple matrix program that prints some data..I want to know how to reduce amount of lines with some functions or whatever, because there is so much if-s,and for loops. Tried to make ...
2
votes
1answer
45 views

Sum of Two Matrices

For a homework assignment, I was asked to do this problem. It worked, but it looks messy in my opinion. I want to know if this is the best way to accomplish this task, and if there can be any ...
3
votes
0answers
48 views

Lowest cost path through elements in a matrix

I've developed a program in Python that calculates the lowest possible costing path between two points in a matrix, including the values contained in the start and finish cells. The code is below. ...
2
votes
1answer
72 views

Computing the difference of the cross-totals of the diagonals of a matrix

I have solved a seemingly easy problem: Given a square matrix, calculate the absolute difference between the sums of its diagonals. For example, the square matrix is shown below: $$\begin{bmatrix}...
4
votes
1answer
137 views

Search for row number of maximum value in matrix column

Background I'm writing code that searches for the row number of the maximum value in a partial column of a matrix. The partial column corresponds with the non-zero values of an equivalent lower ...
3
votes
1answer
81 views

Applying a function to each row of a matrix

My goal is to apply a function func1 to each row of the matrix input and then return a new one resulting from the transformation....
5
votes
1answer
77 views

Manually writing a function matrixpower() in R

Problem 1 - Matrix powers in R R does not have a built-in command for taking matrix powers. Write a function matrixpower() with two arguments mat and k ...
6
votes
1answer
126 views

Matrix class with space-optimized triangular (lower and upper) variants

I've written a class for square numerical matrices (of doubles, in this case), in C++11. Since I intend to use this class later ...
2
votes
1answer
60 views

Applying a function to columns of two matrices producing their combinations

I made a short snippet for applying a function f that takes two arguments (vectors) to columns of two matrices producing all possible combinations of those columns. ...
8
votes
1answer
278 views

Computing a Discrete Cosine Transform matrix

I'm learning Haskell and I needed to work with discrete cosine transform matrices, so I made a program that generates text (usable in WolframAlpha) containing the generated matrix. The elements of ...
4
votes
1answer
60 views

Transpose of a very large matrix with fewer than 1021 rows with Python

I have a matrix of fewer than 1021 rows. I'd like to take its transpose, but it is too big for GNU datamash and awk solutions to ...
2
votes
1answer
43 views

Calculating the potential of a matrix of size (424600, 55)

I have the following working code to calculate the potential of a matrix after normalizing the matrix. But it is too much time consuming. Is there a way to make this code run faster? ...
6
votes
0answers
70 views

Reading sparse matrix from binary file

I have binary files containing sparse matrices. Their format is: number of rows int length of a row int column index int value float Reading ...