The Stack Overflow podcast is back! Listen to an interview with our new CEO.

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
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
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 ...
9
votes
3answers
249 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 ...
6
votes
1answer
627 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 ], ...
24
votes
6answers
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 ...
11
votes
1answer
4k 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 ...
8
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 ...
5
votes
2answers
182 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 ...
3
votes
1answer
428 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 ...
2
votes
2answers
736 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 ...
29
votes
2answers
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 ...
12
votes
1answer
3k 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\$, ...
10
votes
3answers
943 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 ...
6
votes
1answer
785 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 ...
2
votes
2answers
2k 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 ...
2
votes
1answer
979 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: ...
1
vote
2answers
143 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: ...
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. ...
6
votes
1answer
451 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
2answers
421 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 ...
5
votes
1answer
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: <...
5
votes
1answer
81 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...
4
votes
3answers
463 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 ...
4
votes
1answer
7k 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 ...
3
votes
1answer
913 views

Multithreaded matrix multiplication on Unix in C

I have this program for comparing serial and parallel matrix multiplication algorithms: matrix.h ...
3
votes
3answers
246 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 ...
3
votes
4answers
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 ...
3
votes
3answers
6k 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: ...
3
votes
2answers
185 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. $...
3
votes
3answers
2k views

Reversing a matrix in C#

Implementation: ...
2
votes
1answer
126 views

GeeksforGeeks: Rotate matrix by 90 degrees

Please review the code readability, I would like to have an easier to follow code. Thanks ...
2
votes
2answers
815 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....
1
vote
1answer
110 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 ...
1
vote
1answer
851 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 ...
0
votes
2answers
858 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 ...
0
votes
2answers
91 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 ...
0
votes
1answer
78 views

R Function to Generate Predictions from Ratings and Save Results

I am trying to generate predictions using the function below in combination with sapply. My actual datasets are very large, I am attempting to make 1.5 million predictions with this function and it is ...
0
votes
1answer
156 views

Clockwise rotate a matrix in Scala

Given a matrix, clockwise rotate elements in it. Examples: ...
0
votes
2answers
145 views

Calculating shortest possible route between two given nodes

I recently came across this problem: Heroes in Indian movies are capable of superhuman feats. For example, they can jump between buildings, jump onto and from running trains, catch bullets with ...