Skip to main content
Share Your Experience: Take the 2024 Developer Survey

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
9 votes
2 answers
16k 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 ...
Henrik Ståhlberg's user avatar
7 votes
2 answers
2k views

C++20 : N-dimensional minimal Matrix class

I wrote a N-dim matrix (tensor) class, based on my previous 2D matrix implementation(2D Matrix in C++20 and Strassen's algorithm), accepting many helpful reviews from here. MatrixBase.h ...
frozenca's user avatar
  • 1,703
6 votes
2 answers
291 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? ...
Malachi's user avatar
  • 28.6k
5 votes
2 answers
473 views

A Matrix Library in C++;

I have written a Matrix library that contains all the main properties of matrices. It's a relatively long project, I am hoping it is ok to post here since I really want to have it reviewed. The ...
HBatalha's user avatar
  • 355
4 votes
3 answers
602 views

Parse 2D matrix, 2 versions

I'm writing a little C program that computes matrices (for learning purpose). The matrix is fed through arguments in the following form : "{{43,543.324,34},{43,432,87}}" The user doesn't give the ...
Vincent Pasquier's user avatar
4 votes
1 answer
321 views

Snail matrix in Java - version II

(This post is the continuation of Snail matrix in Java. Also, this post has an continuation.) As previously, a snail matrix is any square matrix \$M_n = \mathbb{N}^{n \times n}\$ with cell elements ...
coderodde's user avatar
  • 28.4k
2 votes
2 answers
200 views

2d Grid - Iterating by Rows / Cells - Take #2

Preface Following my first POC version: 2d Grid - Iterating by Rows / Cells I have gained much more understanding, and present A new version A new version is ready, with templates and an attempt at ...
Shigoto Shoujin's user avatar
1 vote
0 answers
88 views

Dictionary based non-local mean implementation in Matlab

Given a dictionary including multiple X-Y pairs where X, Y are both three dimensional structure. dictionaryBasedNonlocalMean function return a ...
JimmyHu's user avatar
  • 5,018
32 votes
2 answers
3k views

A grid and a menu walked into a program

A program that creates a grid 10x10 and assigns a random number in each tile. It then asks you if you want to: create a new grid view the current one count how many of each number there are in the ...
Henrik Bohlin's user avatar
25 votes
6 answers
6k views

2048 merge function

From: Principles of Computing Part 1 Course on Coursera I got -2 pts on my OWLTEST which uses Pylint for style guide. The error states: Too many branches (17/12) function "merge", line 7 ...
Clever Programmer's user avatar
14 votes
4 answers
2k views

Create a snail matrix

Statement: Create a script that, given an integer \$n\$, create a square matrix of dimensions \$n \times n\$ with the numbers from \$1\$ to \$n^2\$, arranged in a snail pattern. Example: ...
Dorian Turba's user avatar
13 votes
1 answer
5k views

Hackerrank Queen's Attack II

Problem statement A queen is standing on an \$n\$x\$n\$ chessboard. The chessboard's rows are numbered from \$1\$ to \$n\$, going from bottom to top; its columns are numbered from \$1\$ to \$n\$, ...
Jianmin Chen's user avatar
  • 2,396
11 votes
1 answer
5k views

Matrix rotation algorithm

This is the Matrix Rotation problem from hackerrank.com. You are given a 2D matrix, \$a\$, of dimension \$M×N\$ and a positive integer \$R\$. You have to rotate the matrix R times and print the ...
benSooraj's user avatar
  • 305
10 votes
3 answers
1k 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 two-dimensional ...
JavaDeveloper's user avatar
9 votes
3 answers
264 views

Matrix reloaded ... or reversed

I have written this Matrix class after posting an answer to Malachi's rags-to-riches question. The initial purpose had been to reverse a matrix represented by a 2D ...
Heslacher's user avatar
  • 50.4k
8 votes
2 answers
439 views

Image Rotation and Transpose Functions Implementation in C++

This is a follow-up question for Gaussian Fisheye Image Generator Implementation in C++ and An Updated Multi-dimensional Image Data Structure with Variadic Template Functions in C++. I am trying to ...
JimmyHu's user avatar
  • 5,018
8 votes
2 answers
3k 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 ...
ChrisB's user avatar
  • 267
7 votes
2 answers
3k 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 ...
Emma's user avatar
  • 3,527
7 votes
1 answer
1k 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 ], ...
Jianmin Chen's user avatar
  • 2,396
7 votes
2 answers
9k 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. ...
JavaDeveloper's user avatar
6 votes
2 answers
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 ...
JavaDeveloper's user avatar
6 votes
1 answer
1k 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 ...
spyr03's user avatar
  • 3,032
6 votes
2 answers
859 views

Snail matrix in Java

(This post has an continuation: Snail matrix in Java - version II.) Inspired by this post, I decided to solve that problem in Java. The idea is to decide on an \$n \in \mathbb{N}\$, create a square ...
coderodde's user avatar
  • 28.4k
6 votes
2 answers
1k 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 ...
Chemistpp's user avatar
  • 227
6 votes
1 answer
800 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 ...
user1235831's user avatar
6 votes
2 answers
163 views

N dimensional array index utility

I created the below class to help working with ND Arrays, mapping based on this question. This will help in implementing the code for handling convolutions. How can I improve upon this? Is there ...
Dávid Tóth's user avatar
5 votes
1 answer
235 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...
Curran McConnell's user avatar
5 votes
1 answer
2k 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: <...
user1235831's user avatar
5 votes
1 answer
423 views

2D Matrix in C++20 and Strassen's algorithm

I wrote C++20 implementation of a simple matrix class and its usage in Strassen's \$O(n^{lg_2{7}})\$ matrix multiplication algorithm. Live demo: https://wandbox.org/permlink/JaSC8fQccFbcl1QY (For n = ...
frozenca's user avatar
  • 1,703
5 votes
2 answers
363 views

Rotating a matrix clockwise by one element without any libraries (Python 3.9)

The question is as follows: Rotate an NxN matrix clockwise by one element and display the new matrix. For example, if ...
st0n3r's user avatar
  • 73
4 votes
1 answer
11k views

Matrix multiplication using functions in C

This exercise surprised me a little bit. I did not expect that gcc (GCC 6.3.0 in the MinGW suite) would use the C11 standard by default, which I realised after I ...
Hungry Blue Dev's user avatar
4 votes
1 answer
2k views

Cache-optimized matrix multiplication algorithm in C

I am trying to optimize matrix multiplication on a single processor by optimizing cache use. I am implemented a block multiplication and used some loop unrolling, but I'm at a loss on how to optimize ...
qscott86's user avatar
  • 141
4 votes
3 answers
939 views

Portable multithreaded matrix multiplication in C

I essentially took this progam, and made it compile and run on Windows too. Also, I incorporated some nice points made by Toby Speight. matrix.h ...
coderodde's user avatar
  • 28.4k
4 votes
1 answer
646 views

HackerRank NCR codesprint: Spiral Message

Problem statement: https://www.hackerrank.com/contests/ncr-codesprint/challenges/spiral-message You've intercepted an encoded spy message! The message originated as a single line of one or more ...
Jianmin Chen's user avatar
  • 2,396
4 votes
2 answers
388 views

How to increase the efficiency of DLA algorithm?

The algorithm: Place seed at the center of the canvas. Release a random walker from the edge. Random walker sticks to the neighboring sites of the seed/previous points. Repeat N(particles) times. ...
CJSSSSS's user avatar
  • 41
3 votes
1 answer
164 views

Snail matrix in Java - version III (generalizing)

Once again, in the previous version, Alexander Ivanchenko helped me with his awesome answer. Now, I have improved the toString() and, also, generalized the class ...
coderodde's user avatar
  • 28.4k
3 votes
3 answers
8k views

Neighbors of a matrix element

I would like a code review to make this code better, clear, and concise. The problem it's solving is as follows: Given a matrix, print the circumference of each of the items. For example: ...
JavaDeveloper's user avatar
3 votes
1 answer
230 views

How to efficiently (fast) calculate the transpose of a *permutation matrix* in Python3?

The permutation matrix is represented as a list of positive integers, plus zero. The number indicates the position of the 1 in that row, e.g. a number zero would mean that the 1 is in the right-most ...
dawid's user avatar
  • 173
3 votes
2 answers
115 views

Converting a natural number to a permutation matrix in Python. How to speed it up perhaps avoiding messing around with lists, sets and dicts?

It is something a bit complex to explain here, but the code bellow takes a 128-bit number and converts it into a permutation matrix, a beast which I have already faced before. The matrix is ...
dawid's user avatar
  • 173
3 votes
4 answers
11k views

Simple matrix class

I have built a 3D matrix class in C++, mainly for data I/O, for a project I am working on. The code works ok, but I would like you to help me improve it. Are there any bad programming practices I am ...
user1995432's user avatar
3 votes
1 answer
126 views

Three-dimensional DCT spatial frequency components illustration

I am attempting to make three-dimensional discrete cosine transformation spatial frequency components illustration. The 3D cubes is used to present the level of each coefficient. The more bright the ...
JimmyHu's user avatar
  • 5,018
3 votes
2 answers
5k views

C++ operator overloading for matrix operations

Please check the code to see if there anything I can improve . Everyting is working as expected. matrix.h ...
benz's user avatar
  • 405
3 votes
1 answer
2k views

Multithreaded matrix multiplication on Unix in C

I have this program for comparing serial and parallel matrix multiplication algorithms: matrix.h ...
coderodde's user avatar
  • 28.4k
3 votes
1 answer
8k views

Generic matrix type in C++

This snippet is about a generic matrix type. The nice part is that given two matrices with respective entry types, say, of short and ...
coderodde's user avatar
  • 28.4k
3 votes
3 answers
4k views

Reversing a matrix in C#

Implementation: ...
Rezo Megrelidze's user avatar
3 votes
2 answers
236 views

Computing even huger Fibonacci numbers in Java

See the next iteration. I have that method for computing Fibonacci numbers \$F_n\$ (\$n = 0, 1, 2, \dots)\$, that relies on computing $$ A = \begin{pmatrix} 1 & 1 \\ 1 & 0 \end{pmatrix}^n. $...
coderodde's user avatar
  • 28.4k
2 votes
0 answers
223 views

2d Grid - Iterating by Rows / Cells - Take #3

Edited with a bit more unit tests. Preface Following my second version: 2d Grid - Iterating by Rows / Cells - Take #2 I believe that I am now satisfied with this new version below. There is still one ...
Shigoto Shoujin's user avatar
2 votes
1 answer
454 views

GeeksforGeeks: Rotate matrix by 90 degrees

Please review the code readability, I would like to have an easier to follow code. Thanks ...
Gilad's user avatar
  • 5,311
2 votes
2 answers
2k views

HackerRank - Queens Attack II - Java

The problem definition can already be found here. Given a chess board with dimensions n × n (where n is up to 100000) and the (r, c) positions of various obstacles, how many squares can a queen at a ...
Koray Tugay's user avatar
  • 1,547
2 votes
1 answer
216 views

Template matrix class, second version

This is my first matrix class which I updated after already asking here already. The updated code is below. Are there any suggestions regarding member function parameters? Is there a convenient ...
Chemistpp's user avatar
  • 227