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
47 votes
1 answer
1k 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 ...
Javran's user avatar
  • 591
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
30 votes
1 answer
23k views

SIMD matrix multiplication

I recently started toying with SIMD and came up with the following code for matrix multiplication. First I attempted to implement it using SIMD the same way I did in SISD, just using SIMD for things ...
Peter's user avatar
  • 451
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
25 votes
2 answers
1k views

The grid looked at the menu and said "Something with class, please"

This is a follow up on my previous question: A grid and a menu walked into a program I integrated classes into the mix. The program now asks how big you want the grid to be. ...
Henrik Bohlin's user avatar
24 votes
4 answers
2k views

Enter the Matrix

I've semi-often been frustrated at the lack of a proper Matrix data structure in VBA. A multi-dimensional array is obviously the right way to handle it, but there is so much missing... for example, ...
Blackhawk's user avatar
  • 1,134
20 votes
4 answers
10k views

Matrix class in C#

I've been learning C# during my free time in the past months; before that, I was mostly writing Java, so the transition hasn't been too hard, but I've never had my code reviewed or read by someone ...
cliesens's user avatar
  • 558
20 votes
1 answer
726 views

Remapping and interpolating 255x1024 array

I have a function that accepts a \$255\times1024\$ array and remaps it to another array in order to account for some hardware related distortion (lines that should be straight are curved by the lens). ...
wes3449's user avatar
  • 303
19 votes
7 answers
2k views

Code to multiply matrices together

I've got a short algorithm used in my code to multiply two matrices and put them into a destination Matrix. I decided to use the temporary array t_elements, which ...
Ann's user avatar
  • 193
19 votes
2 answers
11k 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 ...
Seanny123's user avatar
  • 1,527
18 votes
3 answers
2k views

A library to do maths with matrices written from scratch

Background Some time ago I've encountered some very good articles about neural networks that represented an ANN as a set of matrices, so everything was done using matrix operations. These articles ...
ForceBru's user avatar
  • 455
14 votes
2 answers
7k 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: ...
JavaDeveloper's user avatar
13 votes
5 answers
9k 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 ...
bazang's user avatar
  • 2,236
13 votes
5 answers
4k views

Enter my Matrix

Here is my next Java assignment: create a Matrix class that can add and multiply two matrices together. I was given some code as a skeleton and then fleshed it out ...
syb0rg's user avatar
  • 21.8k
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
12 votes
1 answer
1k views

Looking for a way to create a particular matrix in python in a less amount of time

I want to create a matrix like this in Python: $$ \begin{pmatrix} 0 & 0 & 0 & 0 & 1 & 1 & 1 & 1 & 2 & 2 & 2 & 2 & 3 & 3 & 3 & 3\\ 0 & 1 &...
stressed out's user avatar
12 votes
2 answers
937 views

Python algorithm that converts array-like data into MathJax

I frequently use MathJax, and often I need to write matrices. Since writing matrices using MathJax is very tiresome task, I decided to automate it. Code: ...
Stokolos Ilya's user avatar
12 votes
1 answer
642 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? ...
mitya221's user avatar
  • 793
12 votes
3 answers
5k views

Matrix template with basic operations and utilities

I implemented a basic Matrix class for additions of matrices, multiplication of them, and multiplying every entry of matrix by constant, ...
Incomputable's user avatar
  • 9,454
12 votes
3 answers
2k views

Matrix implementation

I am trying to implement an optimal and fast running matrix in C++. I need some review of the code and ideas on how to improve the code quality if it shall be. ...
Hrant Nurijanyan's user avatar
12 votes
2 answers
3k 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 ...
ohBoy's user avatar
  • 123
12 votes
2 answers
2k views

Quadratic sieve

Here is an implementation I pieced together from various papers on the subject. It took a lot of fiddling to get it to run at all. My ideas for better performance and readability, based on looking ...
qwr's user avatar
  • 1,045
11 votes
4 answers
16k views

Python 3 function to rotate an image 90°

A coding challenge that rotates an image 90 degrees counterclockwise. The image is represented as an array matrix. I believe the time complexity is O(n2), but I'd like to know for sure, as well as any ...
MadHatter's user avatar
  • 837
11 votes
4 answers
5k 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 ...
user avatar
11 votes
3 answers
14k 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 ...
Mikkel Grauballe's user avatar
11 votes
1 answer
2k views

Balanced Random Assignment in Python

It has been a very long time since I've used Python. What I'm looking for: I would like to create a 6-by-6 random matrix where each component is either 1 or 2, so that there are 18 ones and 18 twos. ...
Clarinetist's user avatar
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
11 votes
2 answers
3k 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 ...
Atomix's user avatar
  • 315
10 votes
5 answers
4k views

Matrix multiplication in Java

I am taking input from a text file, performing matrix multiplication, then outputting to another text file. I just want to be a better programmer, so please be a detailed as you want. Comments, naming,...
jermiah's user avatar
  • 115
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
10 votes
2 answers
4k 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 ...
user41918's user avatar
  • 101
10 votes
1 answer
2k views

Matrix multiplication

Below is the code that I've written for matrix multiplication: ...
Gokul Nath KP's user avatar
10 votes
2 answers
5k views

Custom matrix class

It took me while to finish, but here is my custom matrix class. I assume that the row/column iterators are the most critical part of this class but anyway I would very much appreciate your ideas of ...
Vincent's user avatar
  • 561
10 votes
2 answers
191 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 ...
jigsawmnc's user avatar
  • 201
10 votes
3 answers
1k views

How many semi magic squares are there?

I want to write a program to work out how many semi-magic squares there are. Here is the definition of semi-magic squares If we define \$H_n(t)\$ is the number of semi magic squares which satisfy:...
user1024's user avatar
  • 201
10 votes
1 answer
199 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 ...
Amitesh's user avatar
  • 211
10 votes
1 answer
6k views

Print matrix diagonals method

I need to print (or store) all diagonals of a matrix. This includes the main diagonal ( { (i,i) | i = 1, 2, ... min(m,n) } ). Then all super and sub diagonals. For ...
pmdaly's user avatar
  • 253
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
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
9 votes
2 answers
352 views

Implementing a tensor product formula

I would like to use C to implement the following formula: $$\mathbf S(u,v)=\sum_{r=i-p}^{i}\sum_{s=j-q}^{j}N_{r,p}(u)N_{s,q}(v)\mathbf P_{r,s}$$ Namely, $$ \left(N_{i-p,p}(u),\cdots,N_{i,p}(u)\...
xyz's user avatar
  • 217
9 votes
2 answers
4k views

Symmetric Square

I was given an assignment to ensure that a list was symmetric. I built it in a way that worked and it passed the tests. But I wanted to see if it could've been written more efficiently and still ...
chrisdemeke's user avatar
9 votes
1 answer
15k views

Rotate Matrix using Python

This is my solution to Exercise 1.7 from Cracking the Coding Interview. I would appreciate any feedback on coding style and algorithm efficiency. I do know that there is an existing function in ...
Average's user avatar
  • 841
9 votes
1 answer
5k views

Matrix to Tree Algorithm

The code takes a matrix and turns it into a tree of all the possible combinations. It then "maps" the tree by setting the value of the ending nodes to the total distance of the nodes from beginning ...
Colton Allen's user avatar
9 votes
1 answer
3k views

Efficient extraction of patch features over an image

Input: data - a matrix of size n*m*3*3 (complex values) indices - a list of coordinates (x,y), where x < n and y < m fp - a feature parameter which is a tuple of ((fp11, fp12), (fp21, fp22)), ...
Nikolas Rieble's user avatar
8 votes
4 answers
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 ...
streppel's user avatar
  • 635
8 votes
3 answers
548 views

Transpose function [closed]

I wanted to write a transpose function for N*N mat without it getting literals or #define values and I want it to compile with ...
H.cohen's user avatar
  • 189
8 votes
5 answers
8k 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 ...
c0rruptbytes's user avatar
8 votes
3 answers
7k 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 ...
Kevin Meredith's user avatar
8 votes
2 answers
4k views

Read matrix from file, the C++11/14 way

I am trying to get acquainted with C++11/14, so please tell me if the code below could be written in a more "modern" way. Or, of course, if it could be improved in any way. The function ...
Ioanna's user avatar
  • 183
8 votes
3 answers
3k 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 ...
Thibault D.'s user avatar

1
2 3 4 5
14