A message from our CEO about the future of Stack Overflow and Stack Exchange. Read now.

# 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.

570 questions
Filter by
Sorted by
Tagged with
150 views

### Basic Templated Matrix Library

I am a beginner C++ programmer and I have been working on a library to use for working with matrices. The library main features should be Working with matrices (addition, subtraction, arbitrary ...
97 views

### Probabilistic Matrix Inspection - Suggested by a paper, implemented by me

I have read this paper and I actually found it interesting. This is an attempt to implement the proposed algorithm. I would like to know: Is my algorithm correct by what is discribed in the paper? ...
107 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 ...
486 views

### Matrix Diagonal Difference

Problem Given a square matrix of size N×N, calculate the absolute difference between the sums of its diagonals. Input Format The first line contains a single ...
2k views

### Normalize Sparse Matrix along rows in C++ Eigen

I wrote a function to normalize sparse matrix along rows using C++ Eigen; however, I feel that it can be improved, and that I am not using some built in functions of Eigen. ...
478 views

### Linear algebra, reduced row echelon form - function 1

This is a derivative post from here. This is just a general review, so the question is the same: Is there something... That you would consider as a bad practice and why? That is just bad in some ...
553 views

### Householder transformation

I'm trying to make this Householder method run as quickly as possible. It runs fine now, but I'm still a bit new to java, is there anything you guys see to make this run faster? ...
273 views

### Quaternion rotations and preparing matrices for a shader

I am implementing an OpenGL ES 2.0 renderer in c. I want to use quaternions for rotations. Please take a look at the way I am implementing the rotation math. Everything looks as expected when the ...
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. ...
4k 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. ...
721 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 ], ...
557 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 ...
176 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 ...
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): ...
300 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 ...
77 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 ...
75 views

I'm working on 2-D top-down graphics engine, and need a way to simulate clouds. My idea was that I could create a layer comprised of a matrix of rectangles whose alpha value would be picked from a ...
1k views

### Transposing a 2D array

Would you have any suggestions for improvements for the code below? ...
134 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 ...
308 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 ...
1k 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 ...
11k views

### Rotate an image by 90 degrees

Looking for review, good practices, optimizations, clean code tips etc. ...
361 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 ...
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. ...
2k 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. ...
130 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 ...
351 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 ...
270 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 ...
483 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 ...
80 views

### General matrix class

I have written a matrix class using python, allowing me to perform a variety of operations on matrices. I'm pretty happy with how it performs, and everything works fine. However, I feel like the way I'...
156 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 ...
3k views

### Calculate co-occurrences of some words in documents dataset

It's a function to calculate the co-occurrences of some words in a news dataset, such as Techcrunch, Wired. The words list could be locations, products or people names. Words list example: ["New ...
92 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: ...
133 views

### Calculating diagonal difference

I was trying to solve a problem concerning the difference of the diagonal sums of a matrix (using C99): ...
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 ...
96 views

### Rotate a matrix, using transposition and reflection

I want to compare two solutions to the following question from Cracking The Coding Interview by Gayle Laakman McDowell : 1.7 Rotate Matrix - Given an image represented by an NxN matrix, where each ...
72 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 ...
79 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 ...
322 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, ...
116 views

### Mean of the squares of differences for a large matrix

I have tried to obtain speed gains by transferring Matlab calculations to C and call it using .mex. My goal is to operate on a matrix $B$ which has roughly the dimension 10000x1000  \frac{1}{\...
484 views

### In 8-puzzle finding nr of steps moving a tile to the right place

In 8-puzzle I want to count the number of moves it would take to move a specific tile to it's goal place. The problem is my method for this is >100 lines long which is obviously way too much. I'll ...
123 views

### Count number of unique adjacent cells in a matrix

I want to write an algorithm to count how many islands are in a given matrix. Consider for example: ...
374 views

### C++ Dataset class

C++ newbie trying to build a class to represent a dataset for the purposes of building predictive models of the form y ~ x0, x1,... Since this is version 1, I'm assuming some nice properties about my ...
219 views

### Blurring a given image using moving average in Python 3

I've tried solving the following HackerRank question: Write a function blur_image() that takes as a parameter an image in the form of a nested list ...