Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

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.

7
votes
2answers
8k views

Calculate fibonacci in O(log n)

This program calculates the \$n\$th fibonacci number, in \$O(\log n)\$ time. I'm looking for code review, optimizations, and best practices. ...
7
votes
5answers
4k views

Matrices in C implementation

I recently wrote a C implementation of Matrices with add, subtract, and multiply. I want to expand this out eventually where I can diagonalize, efficiently square, row-reduce, etc... I was wondering ...
7
votes
2answers
2k views

Read matrix from file, the C++11/14 way

I am trying to get acquainted with C++11/14, so please tell me if the code below could be written in a more "modern" way. Or, of course, if it could be improved in any way. The function ...
7
votes
3answers
6k views

Searching all diagonals of a 2D M x M array

I've started writing a piece of code to help me search for an object in all the objects found in the diagonals of an M x M 2D array. Though the code works, I'd like to know if there is a way I can ...
7
votes
2answers
4k views

Counting linear conflicts of the state of 8 puzzle for heuristic

I need to find linear conflicts of 8 puzzle state, state is represented by int[9], goal state is {1,2,3,4,5,6,7,8,0}. A linear conflict would be if in a line two ...
7
votes
2answers
1k views

Computing sum on column + sum on row + sum on each diagonal

I have the following problem: I read from a file the dimension(n) of the matrix(which is n * n) and the matrix. For each element ...
7
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 ...
7
votes
2answers
916 views

Generic matrices implementation

I am implementing a litte generic math library. What I have done is to write my generic matrix and vector class. I'm curious if I have it done right so far (implementation wise not totally ...
7
votes
2answers
2k views

User controlled rotation with arcBall

I am trying to visualize a pointcloud centered around the origin. I also want to have a user controlled rotation for which I found the arcball. rMat is the model ...
7
votes
2answers
811 views

Class Matrix implementation

Following the 18.06 Linear algebra course, I was curious to reinvent the matrix class and basic functionality, like \$PA=LU \$ decomposition, Gauss elimination, finding inverse matrix etc. Any ...
7
votes
2answers
79 views

Rotating a matrix in ANSI C

I have this C program that rotates a character matrix. This is a straightforward rewrite of this Java implementation. Both versions do additional \$\Theta(mn)\$ preprocessing and require \$\Theta(mn)\$...
7
votes
1answer
6k views

Divide all elements in the column of a sparse matrix

Using Python Scipy, I am trying to divide all numbers in all columns of a sparse matrix (400K × 500K, density 0.0005), by the sum of the squares of all numbers in a column. If a column is ...
7
votes
1answer
506 views

Optimizing code from spectral subtraction algorithm

This is part of the code from a spectral subtraction algorithm. I'm trying to optimize it for Android. Matlab code: ...
7
votes
2answers
320 views

Utility method to view submatrices

I implemented an utility method that is able to view an Array[n, m] in another smaller dimension [k, l], where ...
7
votes
1answer
763 views

Average of positive and negative part of numpy matrix

I’ve got a matrix of data and I want to take the average of the positive and negative parts of the values. Is there a more Pythonic way of doing it? ...
7
votes
1answer
2k views

Using homogeneous coordinates to rotate an image

Currently, I have this code snippet, but it's a little bit slow: ...
7
votes
2answers
397 views

Counting Islands In Matrix Of Boolean

Code objective: Read a txt file, parse it into a matrix and count (and also print) the number of unique "islands" in the map formed by the matrix. 0 = Sea; 1 = Land; Islands are orthogonal groups of ...
7
votes
1answer
446 views

Strongly connected components, component graph, semiconnected graph and new graph from SCCs

I have written a program in C to do the following: Read a matrix (in file format, first line you have the size, then next are the entries). This is done by the "input" function. Find the strongly ...
7
votes
4answers
6k views

Mathematical matrices implementation

As a Java programmer who's trying his hand at C++, I thought I'd start building a library for working with matrices as my first C++ project. I've got very little C++ experience and it's been a long ...
7
votes
1answer
544 views

Python matrix implementation

I created a simple matrix implementation and would like some critique of it, for example: Things I could add to make it more useful Ways I could make it more efficient (for example __pow__ or ref()) ...
6
votes
2answers
42k views

Given N*N matrix, rotate it by 90 degree to left and right without extra memory

I'm looking for a code review, clever optimizations, and good coding practices. Unit testing has been a visual inspection and concluded expectations match. Also, is the space complexity of the extra ...
6
votes
3answers
7k views

Using a greedy algorithm to find the best path in a 2D array

I'm a student and this is one of my assignments. My professor basically confirmed that my code is correct but all he grades on is if I finished the assignment correctly but not the coding style. I ...
6
votes
3answers
370 views

Inner product of random floats written to file

My goals are to make this code: faster more idiomatic C++ What the code does: take an integer command-line argument N make a vector of ...
6
votes
2answers
6k views

Find largest block in matrix

(Largest block) Given a square matrix with the elements 0 or 1, write a program to find a maximum square submatrix whose elements are all 1s. Your program should prompt the user to enter the ...
6
votes
3answers
4k views

Implementing Transpose

I'm implementing transpose in Haskell. -- transpose [[1,2,3],[4,5,6],[7,8,9]] -- [[1,4,7],[2,5,8],[3,6,9]] Please give it a ...
6
votes
2answers
284 views

First template class - Matrix functions

This post has been answered and updated. This was the first time I've ever written a template class. I've spent probably a bunch of times re-writing matrix code for different types, different ...
6
votes
2answers
1k views

Find element in sorted matrix

Find an element in a MxN matrix where each row and column is sorted in ascending order. Any comments on my code? (I added some test cases on non square matrices which I did not copy here) ...
6
votes
1answer
6k views

4×4 matrix multiplication

This is a simple C++ code with a function mult to multiply matrices. This can easily be generalized for any \$n \times n\$ matrix by replacing ...
6
votes
2answers
3k views

AVX SIMD in matrix multiplication

I have coded the following C function for multiplying two NxN matrices and using AVX vectors to speed up the calculation. It works but the speedup is not what is to be expected(some scalar code is ...
6
votes
3answers
3k views

Finding clusters in a matrix

I got asked at an interview to write a program that, given a NxM matrix with zeros and ones, prints out the list of clusters of 1s. The clusters are defined as patches of 1s connected horizontally, ...
6
votes
3answers
3k views

Rotating an NxN matrix

I came up with the following solution for rotating an NxN matrix 90 degrees clockwise, to solve this CodeEval challenge: Input The first argument is a file that contains 2D N×N matrices (where ...
6
votes
2answers
190 views

Reversing a Matrix

I took the idea from Reversing a matrix in C# and decided to write my own matrix-reversing code. Is there anything that I can do to make it even better? ...
6
votes
2answers
723 views

Increment the neighbors of a cell in a matrix

Let's say you want to increment the cells around the position (x,y) in a square matrix m of size ...
6
votes
2answers
2k views

Compute spherical distance matrix from list of geographical coordinates

I've got a list of geographic coordinates ([lat, long]) and want to compute the corresponding matrix of distances. Distance shall be spherical distance in km. <...
6
votes
1answer
1k views

Compute weighted average around a point in a matrix quickly

I have a short snippet of code that simply computes a weighted average for surrounding elements in a square matrix. The actual implementation that I'm working on is not an average (more complex ...
6
votes
2answers
641 views

Gauss elimination

The gauss function takes 3 parameters: a pointer to the matrix of coefficients the number of variables a pointer to matrix of solution ...
6
votes
1answer
410 views

Optimizing multiplication of square matrices for full CPU utilization

Problem I am learning about HPC and code optimization. I attempt to replicate the results in Goto's seminal matrix multiplication paper. Despite my best efforts, I cannot get over ~50% maximum ...
6
votes
1answer
1k views

numpy performance of norm calculation using variable dimension

I am looking for advice to see if the following code performance could be further improved. This is and example using a 4x3 numpy 2d array: ...
6
votes
1answer
1k views

Determine if an element exists in a sorted NxN matrix

The point of this algorithm is to see if an element exists in a NxN matrix that has its rows and columns sorted. What would you change? What did I do well? Both perspectives help so I am not left ...
6
votes
1answer
113 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 ...
6
votes
1answer
140 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 ...
6
votes
2answers
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? ...
6
votes
2answers
426 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 ...
6
votes
1answer
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. ...
6
votes
1answer
416 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 ...
6
votes
1answer
203 views

The Last Lost Bubble Shooter - a Board game for children

In this game the pieces are trains and you move forward on the track (never backwards) in search of your goal. The players move based on a combination of resources (Soap and Water to make Bubbles) ...
6
votes
1answer
74 views

Perl gradient noise generator

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 ...
6
votes
1answer
474 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? ...
6
votes
2answers
261 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 ...
6
votes
1answer
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. ...