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.

learn more… | top users | synonyms

25
votes
0answers
313 views

Capture the notion of invertible functions

I find sometimes it is useful to capture the notion of invertible functions. The idea is that if two functions f :: a -> b and ...
13
votes
1answer
132 views

Finding a zero crossing in a matrix

I am trying create an algorithm for finding the zero crossing (check that the signs of all the entries around the entry of interest are not the same) in a two dimensional matrix, as part of ...
11
votes
4answers
1k views

“Two-pass algorithm” implementation in Java

I'm rather new to Java and to algorithms in general. I implemented the so called Two-pass algorithm, and want to know if there are improvements to my way of implementing it. What it does is it takes a ...
11
votes
5answers
512 views

Set matrix zeros

I solved this problem in about 11 minutes (with all 50+ test cases passed). I think most of you already know me by now, and I truly appreciate your advice, but please be brutal and judge me as if I ...
10
votes
2answers
399 views

Finding average of eight immediate neighbors of a matrix

I have this basic Java code to find average of eight immediate neighbors of a matrix. Is there any way to simplify or merge any part of it, or can I refactor it? I'm a beginner in Java programming ...
10
votes
3answers
333 views

Find biggest basin

Problem Statement A group of farmers has some elevation data, and we’re going to help them understand how rainfall flows over their farmland. We’ll represent the land as a ...
10
votes
3answers
321 views

Row/Column Transpose

I was wondering if there is a smarter way of doing the following code. Basically what is does is that it opens a data file with a lot of rows and columns. The columns are then sorted so each column is ...
10
votes
1answer
547 views

Matrix multiplication

Below is the code that I've written for matrix multiplication: ...
10
votes
2answers
134 views

How to increase efficiency of matrix operation in C?

I have a N*N upper triangular matrix with property such that, all its diagonal elements are a1,a2,a3,...,aN. I want that a[i][j] (for all j>i) should be ...
9
votes
2answers
224 views

Count the one islands in the matrix

What is an island? A group of connected 1s forms an island. For example, the below matrix contains 5 islands: ...
9
votes
1answer
81 views

Gauss-Seidal implementation

The code is an implementation of the Gauss–Seidel method. I would like a general review of the code. PS: I use code::blocks IDE ...
9
votes
2answers
263 views

C struct or pointer to struct?

I am currently using this code (and more) to deal with 2D matrices. I am not very proficient in C, so I am not quite sure, if this is a good way of doing it. I've seen other people in similar ...
8
votes
1answer
165 views

10x10 bitmapped square with bits

This program prints out a 10x10 square and uses only bit operations. It works well, but I don't like that global array. Can you tell me if the code is proper or not? ...
7
votes
2answers
202 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
708 views

Repetitive For Loops in 2048 Game

I have the following function defined in a program that implements the 2048 game. I tried condensing the repeating part of the code into one loop containing +1 offset variables for the indices, which ...
7
votes
1answer
296 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: ...
6
votes
1answer
74 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
618 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
70 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. ...
6
votes
3answers
54 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 ...
6
votes
1answer
44 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? ...
6
votes
1answer
419 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 ...
5
votes
3answers
78 views

More efficient (or correct) way to make image grouping work?

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 ...
5
votes
2answers
1k 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 int[16]s. ...
5
votes
2answers
134 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. ...
5
votes
2answers
261 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? ...
5
votes
2answers
52 views

Increment the cells around one in a matrix

Let's say you want to increment the cells around the position (x,y) in a square matrix m of size ...
5
votes
1answer
80 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 ...
5
votes
1answer
100 views

Optimizing multiplication of square matrices for full CPU utilization

Cross-post from Stack Overflow since this is very code intensive Problem I am learning about HPC and code optimization. I attempt to replicate the results in Goto's seminal matrix multiplication ...
5
votes
2answers
82 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
1answer
81 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
107 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 ...
5
votes
1answer
57 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
131 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 ...
4
votes
2answers
477 views

Rotate an image by 90 degrees

Looking for review, good practices, optimizations, clean code tips etc. ...
4
votes
1answer
56 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
328 views

The number of connected component

Compute the number of connected component in a matrix, saying M. Given two items with coordinates [x1, y1] and [x2, y2] in M, if M(x1, y1) == -1 and M(x2, y2) == -1 and |x1-x2| + ...
4
votes
1answer
592 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 ...
4
votes
1answer
84 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 ...
4
votes
1answer
52 views

Matrix rotation efficiency

I am not sure if I should be using recursion or not or if it even helps in increasing the performance of my algorithm. I feel as though I am creating and replacing too many arrays by calling my inner ...
4
votes
1answer
128 views

Producing a matrix class that supports any size

I'm doing exercise 11.8 from Scala for impatient, asking to write a matrix class: Provide a class Matrix - you can choose whether you want to implement 2 x ...
4
votes
2answers
1k 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 ...
4
votes
1answer
55 views

Optimization of matrix determinant calculation

I have this algorithm that calculates the matrix determinant using recursive divide-and conquer-approach: ...
3
votes
3answers
204 views

Simple matrix class - version 2

As suggested on my previous question, I am posting my revised code for feedback. One thing that caught my attention is that my program actually runs slower after adding the modifications. It was ...
3
votes
2answers
402 views

Templated Matrix class

I created a Matrix class. I tested it and it seems to work. Can someone tell me if this class is good? Can I improve it? Can I use more move semantics (where)? What do I have to modify? Are there ...
3
votes
5answers
313 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. ...
3
votes
2answers
93 views

Path from source to destination, with increasing values

Given a source and destination, in an integer matrix, find path, such that the next position in the path has a greater than or equal to value than the previous position. In other words ...
3
votes
2answers
42 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 ...
3
votes
2answers
322 views

Find mirror image of a matrix

Looking for review, good practices, optimizations, clean code tips etc. ...
3
votes
1answer
74 views

Point in a polygon algorithm

I am implementing a Point in polygon algorithm. Inputs: M, N: size of the matrix poly: a ...