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
5
votes
1answer
219 views

Creating a special Toeplitz matrix in R

I'd like to know if there is a standard way of creating a matrix like the one shown below using the R programming language. $$ \begin{bmatrix} 1 & \rho & \rho^2 & \cdots &...
5
votes
2answers
186 views

Optimizing garbage collection for local objects

I am trying to make a 3D application with OpenGL/LWJGL at the moment, but I am now already hitting some limits on the JVM, most likely due to a configuration that needs to be done. I have the ...
5
votes
2answers
3k views

Finding the largest product of four consecutive numbers in a grid

Project Euler #11 asks to find the largest product of four numbers of a grid, where the four numbers occur consecutive to each other vertically, horizontally, or diagonally. Here is my solution in ...
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...
5
votes
1answer
417 views

A Simple Interactive Program that Can Solve A Linear System of Equations

So I was just reading my algebra-precalculus textbook, and learned that matrices can be used to solve systems of equations. Since the whole process seemed so algorithmic, I wondered if I could ...
5
votes
2answers
296 views

Array transposition

The task: take a byte[][] and transpose it. The complication: the original array is a result of a partition operation that might not be even, so the last subarray ...
5
votes
1answer
4k views

Java Matrix multiplication using Multithreading

I am trying to achieve matrix multiplication using the concept of concurrency and cyclic barrier. Here is the code snippet:- ...
5
votes
1answer
977 views

Two-step OpenCL convolution for series of matrices

I've implemented a two-step convolution in OpenCL running on GPUs. The convolution is applied to a series of 1480x1552 matrices. All matrices are pre-loaded and are stored in the ...
5
votes
1answer
410 views

Sparse matrix multiplication for billion by billion matrices

I have to do multiplication of two billion by billion sparse matrices (on a CPU), hence any help or hints in optimizing the below given code would be extremely useful. Note: I am only showing the ...
5
votes
2answers
177 views

Computing the RPY tensor with C++/Eigen (linear algebra)

I'm mostly used to writing scientific code in MATLAB / Python / Julia and my experience with C++ is very limited. I've implemented a fairly simple computation of the Rotne-Prager-Yamakawa tensor in ...
5
votes
1answer
2k views

Transpose a large matrix in Python3

I want to transpose this matrix input: 1.000 2.00 3.0 4.00 5.00 6.000 7.00000 8.0000000 9.0 10.0 11.0 12.00000 And get this output: ...
5
votes
1answer
2k views

Linear algebra, reduced row echelon form

The code that I am sharing here for you to review today, is a segment of a JavaScript library that I am going to write as time goes by for fun. It is only the two functions in the following code: <...
5
votes
1answer
67 views

Orchard Partitioning in to close-to-equal sectors

The task (from this site) is to create a program that will solve a problem where a matrix (size n * n), which contains integers, is to be divided into four parts, the sums of which are as close to ...
5
votes
2answers
626 views

Defensive programming type-checking

I have issues with dynamically typed languages, and I tend to worry about type a lot. Numpy has different behaviour depending on if something is a matrix or a plain ndarray, or a list. I didn't ...
5
votes
1answer
171 views

Processing random matrices

I am doing my research about scheduling by using Matlab. I am a new Matlab user and I have a problem with time execution of my following code: ...
5
votes
1answer
4k views

Matrix multiplication and dot-product

Exercise 2.37. Suppose we represent vectors v = (vi) as sequences of numbers, and matrices m = (mij) as sequences of vectors (the rows of the matrix). For example, the matrix is ...
5
votes
1answer
921 views

Type safe, easy to understand matrix multiplication

I always hated matrices (as they are taught in those linear algebra course books that I have browsed), because it's an obscure way of saying "a tuple of linear functions of same arity". It is very ...
5
votes
1answer
821 views

Improving the manipulation of 2D Arrays for an easy challenge on HackerRank with Python 3

I am practicing some exercises on HackerRank in order to improve my skills on Python3. I am doing the 30 days challenge. There is one challenge involving matrices. I was able to pass the test cases ...
5
votes
1answer
147 views

Reading a matrix and computing the determinant

As one of my first C programs I want to read in a matrix and compute its determinant. I don't pose limits on the size of the matrix and this makes things more complicated. Version 0 ...
5
votes
1answer
380 views

Translating array pointer access from C++ to Delphi

I'd like to know if I translated a piece of code correctly from C++ to Delphi. It looks like it is working, but I have a feeling that I'm reading and writing into memory that I'm not supposed to using ...
5
votes
2answers
1k views

Golang solution to Project Euler #81 (min path through a matrix)

Project Euler problem 81 asks: In the 5 by 5 matrix below, ...
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 ...
5
votes
0answers
513 views

Codewars: N-dimensional Von Neumann Neighborhood in a matrix

Task: For creating this challenge on Codewars I need a very performant function that calculates Von Neumann Neighborhood in a N-dimensional array. This function will be called about 2000 times The ...
5
votes
0answers
207 views

Cache conscious SIMD matrix multiply of unsigned integers

The goal of the code review by order of importance (i.e. What I hope to hear from you): I've verified correctness using a straightforward matrix multiply function though I am open to those who want ...
5
votes
0answers
130 views

Kernel density regression in Julia

Here is a naive implementation of a kernel density regression algorithm in Julia. The code works, but there is quite a bit of room for improvement though. First, there is not an optimal bandwidth ...
4
votes
3answers
2k views

Kadane's Algorithm for 2D array with known boundaries

I asked this question first on StackOverflow but I didn't get an answer and was advised to try here too. So here we go. I have implemented Kadane's algorithm for a 2D array in Python 2 with known ...
4
votes
3answers
3k views

Project Euler 81 (minimum path sum through a matrix)

Problem Statement: In the 5 by 5 matrix below, 131 673 234 103 18 201 96 342 965 150 630 803 746 422 111 537 699 497 121 956 805 732 524 37 331 ...
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 ...
4
votes
3answers
494 views

Presidential election predictor using matrix-processing

I'm currently working on a project where I am trying to predict a presidential election based on the population density and region of a voter. I wrote a few MATLAB functions to parse the data, but it ...
4
votes
5answers
2k views

Search algorithm for specific values in a 3D array/matrix being super slow

I'm trying to create code that reads a text file (containing (double) numbers between 0 and 1) and filling up a 3D array/matrix (calling it matrix from now on) with ...
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 ...
4
votes
2answers
594 views

Compute the XOR of all the elements in the upper left triangular of a square matrix

Given a dimension \$D\$. We generate a \$D\times D\$ matrix with elements that start from \$0\$. For example, if we're given a dimension \$4\$ this is what our matrix looks like: $$ \begin{bmatrix}...
4
votes
3answers
978 views

Computation of inter-grid distance array for 2D lattice

I have a 2D lattice with U × U number of grids. My goal is to create an array – of shape U^2 × U^2 – where each entry denotes the least-distance between each grid and any other grid. Currently, for ...
4
votes
2answers
217 views

Increase performance of a spate of 2x2 matrix multiplication

I would like to improve the performance of the piece of code below (in Fortran). It gives good results but the profiling tells me that it is where it spends most of its running time. Basically, it ...
4
votes
2answers
2k views

4x4 Sudoku solver performance

The code is for a 4x4 Sudoku solver. It works fine when there are a small number of unfilled spaces (0's) but when I give the whole matrix input as 0's or so the solver takes a very long time. I need ...
4
votes
1answer
99 views

Performing a special multiplication on two square matrices

I have a function called mult2 that takes in two square NumPy matrices and returns a matrix that is the result of a special multiplication: ...
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 ...
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. ...
4
votes
2answers
700 views

If zero found in Matrix. make entire row & column zero

Problem is - If in an MxN matrix, a zero is encountered, the entire row and entire column should be set to zero. Example - 8 4 2 2 3 7 1 0 1 9 8 4 ...
4
votes
2answers
677 views

Hardcoding matrix multiplication

I was doing a problem, SPOJ: FLIB. My code was running slow (getting Time Limit Exceeded error) when I was using Code1. However, my code accepted within the required time limit when I hardcoded the ...
4
votes
2answers
2k views

Optimize Scipy Sparse Matrix Factorization code for SGD

I'm working on implementing the stochastic gradient descent algorithm for recommender systems (see Funk) using sparse matrices with Scipy. This is how a first basic implementation looks like: ...
4
votes
3answers
170 views

Manipulating matrices for 3D drawing

I've written a short library for manipulating matrices for 3D drawing. I've tried to strike a balance between speed and readability. Anything to improve? ...
4
votes
1answer
474 views

High performance exponential of a skew Hermitian matrix in Fortran 95

I'm new to Fortran, and this is pretty much my first escapade. Below is a function that I wrote which relies on calls to LAPACK. The function is sat in a module with some other functions and works ...
4
votes
1answer
463 views

Back propagation neural network

This is the first time I tried to write a back propagation ANN and I would like to know what more experienced people think of it. The code is meant to distinguish if text is written in English, French ...
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 ...
4
votes
1answer
7k views

Matrix multiplication using functions in C

This exercise surprised me a little bit. I did not expect that gcc (GCC 6.3.0 in the MinGW suite) would use the C11 standard by default, which I realised after I ...
4
votes
3answers
463 views

Portable multithreaded matrix multiplication in C

I essentially took this progam, and made it compile and run on Windows too. Also, I incorporated some nice points made by Toby Speight. matrix.h ...
4
votes
2answers
333 views

Multithread the variant of matrix multiplication

This is matrix multiplication. Big matrix split on the submatrix and multiplication in parallel, result matrices merge to result. How i can make this code better? ...
4
votes
3answers
1k views

2D Vector: Loading values from .txt file

I've written a program that runs through a matrix of numbers, ranging from 0 to 2, and stores the results in a 2D vector. I'm using the values in the 2D vector as the profile for a tile map for a ...
4
votes
3answers
450 views

2D matrices addition

I have a 2 dimension matrix : ...