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
8
votes
1answer
301 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) ...
8
votes
1answer
96 views

Returning multiplied matrix array

Is there a more clever way of writing this piece of code? It does work but I thought there might be a better way of returning a matrix from multiplying the parameter by itself. ...
8
votes
1answer
520 views

Ring datatype in Python

I wanted to write something that would be useful for the indices of a grid where if one walked off the edge of the grid they would re enter on the opposite side. This could in theory help with ...
7
votes
3answers
12k views

JavaScript solution for diagonal difference

hackerrank.com - diagonal difference: Problem Statement You are given a square matrix of size N×N. Calculate the absolute difference of the sums across the two main diagonals. Input ...
7
votes
4answers
2k views

Matrix struct with random values allocation and deallocation

I've written this code in 2013 or 2014, I think. I've rediscovered it today in one of my folders, made some changes and now I'm left (and my 2013/2014 me as well) wondering how good it looks for other ...
7
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 ...
7
votes
2answers
1k views

Triangle area computation and linear transformations

I have a task to learn how to write clear and understandable documentation of written code in python. Please, can you give me a feedback to attached code below? I need to know if the code and comments ...
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
2answers
5k 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 ...
7
votes
5answers
5k 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
846 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 ...
7
votes
3answers
7k 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
5k 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
2k 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
567 views

Pad a ragged multidimensional array to rectangular shape

For some machine learning purpose, I need to work with sequences with different lengths. To be able to process efficiently those sequences, I need to process them in batches of size ...
7
votes
2answers
1k 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
1k 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
87 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
520 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
433 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
174 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 ...
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
857 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
2answers
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 ...
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
441 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
464 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
1answer
608 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
43k 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
373 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
2k views

Efficiency and techniques for Matrix class that inherits from Python's list

I have a class named Matrix, this class inherits from Python's list. The class currently can ...
6
votes
2answers
7k 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 number of ...
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
739 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
1answer
9k 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
143 views

MPI communicator creation exercise

I'm working on an assignment which entails creating communicators following the diagonals of an n x n process grid. I'm interested to get feedback regarding the correctness of the solution I developed ...
6
votes
3answers
4k 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
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
3answers
3k 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. ...
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
198 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
898 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
3k 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
761 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
275 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 ...
6
votes
1answer
419 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
3answers
160 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 ...
6
votes
2answers
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: ...

1
2
3 4 5
12