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

43 questions
Filter by
Sorted by
Tagged with
254 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 ...
200 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? ...
339 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 ...
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 ...
252 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 ...
808 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 ], ...
5k 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 ...
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 ...
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 ...
1k 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 ...
548 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 ...
1k 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 ...
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 ...
4k 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$, ...
971 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 ...
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. ...
866 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 ...
199 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. ...
1k views

### SciPy sparse: optimize computation on non-zero elements of a sparse matrix (for tf-idf)

I have a sparse matrix (term-document) containing integers (word counts/tf) and I am trying to compute the tf-idf, for every non-zero value in the sparse-matrix. The formula for tf-idf I am using is: ...
3k views

Please check the code to see if there anything I can improve . Everyting is working as expected. matrix.h ...
269 views

### Print matrix in spiral order in Scala

Problem: Given a square or rectangular matrix, print its element in spiral order. For example, if input matrix is this: ...
1k views

### Effective Python 2/3 two-dimensional array initialisation

Recently I noticed that the idiomatic python two-dimensional array initialisation is extremely slow. I am wondering, is there a good, proper way of doing this simple task fast? Also, are those two ...
803 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 ...
547 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 ...
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: <...
106 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...
621 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 ...
9k 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 ...
197 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. \$...
55 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 ...
47 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 ...
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 ...
2k views

### Reversing a matrix in C#

Implementation: ...
1k views

### Multithreaded matrix multiplication on Unix in C

I have this program for comparing serial and parallel matrix multiplication algorithms: matrix.h ...
7k 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: ...
819 views

### A matrix with square diagonals and transpose being increments of other

Construct a matrix with the following property: North-west to South East diagonals are squares. Matrix[i][j] + 1 = Matrix[j][i] for each i less than j. Example of such m a matrix is here. Also, ...
213 views

### GeeksforGeeks: Rotate matrix by 90 degrees

Please review the code readability, I would like to have an easier to follow code. Thanks ...
150 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 ...
1k views

### Rotating a matrix in Java

Introduction This program is about rotating a matrix. For example, let the matrix be abcde fghij klmno pqrst Now, after rotating two steps clockwise we will ...
368 views

### Clockwise rotate a matrix in Scala

Given a matrix, clockwise rotate elements in it. Examples: ...
92 views

### Code for number of routes possible of the given length between 2 given nodes

Recently I came across this problem , here is an excerpt of it, It is well known that the routing algorithm used on the Internet is highly non-optimal. A "hop", in Internet jargon, is a pair of ...