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.

540 questions
2k views

Calculating Euclidean norm for each vector in a sparse matrix

Below is a naive algorithm to find nearest neighbours for a point in some n-dimensional space. ...
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 ...
3k views

Gaussian Elimination with Pivots

I'm pretty new to python, and coding in general. For practice, I've written the following code, which uses Gaussian reduction to solve a system of linear equations. ...
161 views

Linear algebra module

I'm working on a linear algebra module to improve my knowledge with mathematics and, because I'll need a lightweight linear algebra module for my future work with Vulkan! I tried to keep a blas-like ...
548 views

3D matrix rotation in homogeneous coordinate space

Assuming that the framework is in place to handle the difference between row and column major matrices, I am curious to know if in a header based library such implementation is semantically and ...
2k views

Implementing the CutHill-McKee Algorithm

I am very much interested in the Reverse Cuthil McKee Algorithm. I have seen Fortran and C or C++ implementations of it, and I decided that it would be a nice exercise to implement it in Python. I ...
155 views

Multivariate normal density using Woodbury matrix inversion lemma

I'm trying to speed up by multivariate normal density function by taking advantage of the fact that the covariance matrix is of the form A + U C U'. The inverse of such a matrix can be calculated ...
567 views

Leetcode 54: Spiral Matrix

Problem statement Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example, Given the following matrix: [   [ 1, 2, 3 ], ...
2k views

Extending numpy array by replacing each element with a matrix

Suppose I have a H*W numpy array like this (H=3 and W=2): ...
247 views

Encryption/decryption by matrix multiplication in C

I need to encrypt and decrypt text, image and video with a program in C with a matrix. My program is working but it's really slow, how can I improve its speed ? specially for the decrypt part. Each ...
1k views

Sparse matrix compressed sparse row (CSR) in Python 2.7

Brief introduction for CSR: The compressed sparse row (CSR) or compressed row storage (CRS) format represents a matrix M by three (one-dimensional) arrays, that respectively contain nonzero ...
45 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 ...
1k views

Transposing a 2D array

Would you have any suggestions for improvements for the code below? ...
131 views

Image-grouping using bit matrices

It might be easier to read the explanation of what I was trying to do, and implement it a completely different way, rather than go through the code. But the code does what I want it to do, just ...
8k views

Am I multiplying two matrices together correctly?

I just wanted to know if I'm doing this right. Note that all the matrices are ints. ...
259 views

Create matrices of a certain format

I need to create matrices that follow a specific format: the matrix is a square NxN matrix and the off-diagonals all have the same value (param) and the diagonals ...
988 views

Are there any memory issues with this Eigen QR wrapper?

I am writing a wrapper to Eigen QR for my personal use and I am wondering if there are any memory leaks or undocumented behavior in my implementation, especially in the function ...
2k views

Fixed size matrix implementation

I implemented a fixed-size matrix class which supports basic matrix operations (cannot use C++11). What do you think, and how could it be improved? ...
2k views

Finding the maximum row of a 4x4 matrix

I have written a recursive function that I believe will return the 'maximum' row of the 4x4 matrix it is fed. By 'maximum' I mean that the maximum row of the matrix. ...
1k views

Matrix Implementation using struct in C

Follow up on this post. I have refactored my code and created a matrix_t struct and implemented various functions to create, delete, modify and perform arithmetic on them. ...
109 views

Simple matrix library in C++

Today I started learning C++ and at the end of the day have made a simple generic matrix class. I'm looking for feedback on techniques and features. It's still not complete, though. But everything ...
333 views

Printing an N*N array in the rectangular/circular/spiral fashion

The problem is to print an N*N array and fill it with numbers(starting from 1 at (0,0)) in the rectangular/circular/spiral fashion, going inwards, ending at the N*N value. Also find and count the ...
264 views

Immutable Matrix

I'm writing implementations of some numerical methods to solve linear equations systems, those implementations use the following Matrix class. I'm trying to get this class immutable, due to that the ...
447 views

Dynamic matrix in web browser

Today, my project was to make a matrix in my web browser. It is really slow, so help with optimizing it would be nice. It runs slow on my PC, yet alone my iPod 4 which this is going to be for. ...
1k views

Path sum - Dijkstra's algorithm in F#

I'm learning F# and I've decided to solve Project Euler's Problem #81 with Dijkstra's algorithm. In the 5 by 5 matrix below, the minimal path sum from the top left to the bottom right, by only ...
154 views

Finding black cluster size distribution for square grid at different probabilities of black elements (averaged over a certain number of iterations)

Context: I had asked a related question on Stack Overflow. On receiving several helpful hints from the commentators there, I could successfully implement averaging of the size distribution over a ...
91 views

Extract the largest value for each day from a matrix

I have a matrix in which the right-most elements are repeated YYYYMMDD dates in descending order, for example: ...
127 views

Calculating diagonal difference

I was trying to solve a problem concerning the difference of the diagonal sums of a matrix (using C99): ...
1k views

Spiral Scanning for matrix using recursion

I have written code for scanning an N x M matrix in spiral order using a recursive function. It uses 5 parameters - including 2 extra variables: call to keep ...
4k views

Optimize vector rotation

I have a trivial function that rotates 2d vectors, and a method in a class representing a polygon that rotates every point in the polygon around an origin. The code is fairly optimized as it is, but I ...
38 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 ...
66 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 ...
182 views

Determinant of a matrix of doubles

I have been working with some matrix functions and need the determinant. I have looked at several examples, but they all seem to use a hard coded size for the matrix, or calculate for a matrix of int, ...
736 views

Matrix rotation algorithm - follow-up

In a response to this post I wrote a solution in Python. It passed all the test cases given, so I would like help making it more pythonic and reducing the almost repeated lines. Link to the source of ...
116 views

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 ...
2k 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 ...
64 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...