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
2
votes
1answer
167 views

Matrix of constrained sums using R

In this post, I asked a question regarding how to create a matrix of integers where elements in each row satisfy a particular property. The notation in the original post is a bit cumbersome so I ...
2
votes
0answers
108 views

Online Principal Component Analysis with spectral bounds

I have question regarding online PCA implemented in the paper. I am interested in the Algorithm 2. I am not certain if there is a bug in my code can someone confirm if my code is correct? The ...
2
votes
1answer
3k views

Swapping the diagonals of a matrix [closed]

I am required to write a simple matrix program and swap the diagonal. Given the input 1 2 3 4 5 6 7 8 9 After swapping the diagonal it should be ...
4
votes
3answers
994 views

2D Vector: Loading values from .txt file

I've written a program that runs through a matrix of numbers, ranging from 0 to 2, and stores the results in a 2D vector. I'm using the values in the 2D vector as the profile for a tile map for a ...
4
votes
2answers
3k views

4x4 matrix implementation in C++

I've been doing some 3D graphics in OpenGL lately and I needed a way to work with 4x4 matrices. My implementation supports the following operations: Matrix-matrix addition. Matrix-matrix subtraction. ...
3
votes
1answer
898 views

Building a matrix by applying XOR rules

There is a matrix of \$m\$ rows and \$n\$ columns where each row is filled gradually. Given the first row of the matrix we can generate the elements in the subsequent rows using the formula: $$\begin{...
0
votes
2answers
141 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 ...
0
votes
2answers
90 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 ...
2
votes
1answer
124 views

Internal method to calculate the distance between all nodes in Kruskal's Importance (Driver) Algorithm

I've been trying to reduce the indentation from a method in my code. The solution will probably be unrelated to the language, in this case python, although it may rely on some python specific ...
5
votes
0answers
193 views

Cache conscious SIMD matrix multiply of unsigned integers

The goal of the code review by order of importance (i.e. What I hope to hear from you): I've verified correctness using a straightforward matrix multiply function though I am open to those who want ...
3
votes
1answer
4k 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 ...
2
votes
2answers
4k views

C++ Matrix Multiplication Using Classes

I'm trying to write a C++ multiplication program using classes (for the first time - I usually try to avoid OOP) and am having trouble freeing my memory. What I'd ideally like is a matrix class with a ...
5
votes
1answer
85 views

Calculating how similar two objects are according to a database

I want to calculate how similar two objects are according to a database. However the code is very slow; it takes around 2 minutes to analyze just 3 objects. How can I speed it up? I have tried to ...
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 ...
4
votes
1answer
173 views

Multiplying two- or three-dimensional arrays with broadcasting

Background Disclaimer: Skip if you are not interested in the background. The code is far far simpler than this. The problem is more about programming than math. Here is the definition of ...
-2
votes
2answers
104 views

Numerical algebra using a Matrix class [closed]

I would like to make a Matrix class. Can any of you help me about this class? There are two ways: ...
6
votes
2answers
126 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 ...
2
votes
2answers
1k views

'Zero out' a matrix using Python

Below is my code for an attempted solution to cracking the coding interview exercise 1.8 written in python 3.5. The problem statement is: Write an algorithm such that if an element in an MxN ...
10
votes
1answer
5k 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 ...
5
votes
1answer
9k 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 ...
1
vote
2answers
53 views

Generate Orders from Matrix and save as CSV

I had a task to create a VBA macros which will take a matrix data set with products in rows and persons as a columns (on intersection we have an ordered qty for each material for each person) and ...
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 ...
3
votes
1answer
646 views

Exporting a table with header and subheader

I have an array matrix, for example A(3,3), I want to make a table with header names like, ['force','mass','acceleration'], the second row contains the units (subheader), ['N','Kg','m/s^2'], and the ...
3
votes
2answers
697 views

Largest area of identical adjacent matrix elements

Given a 2D matrix find the largest area of identical elements. Here is my first implementation: ...
7
votes
2answers
346 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 ...
2
votes
2answers
962 views

Allocate N-dimensional array in C++

I need to allocate n-dimensional arrays of type T, currently I am using the following functions. Is there a better way to do this? Also, ideally I would like to allocate a 1-D array and access that as ...
3
votes
1answer
550 views

Search in sorted matrix NxM

The problem (from Cracking the Coding book): Given an NxM matrix in which each row and each column is sorted in ascending order, write a method to find an element. I thought of a modified version ...
4
votes
1answer
3k views

Conditional removal of columns in sparse matrix

I have a large dataset (78k instances x 490k features) that is loaded as a scipy.sparse.csr_matrix format. From this dataset I want to filter certain features (i.e. ...
2
votes
1answer
3k views

Pattern searching in 2d grid

This is an interview question which i am trying to solve. You are given a 2D array of characters and a character pattern. WAP to find if pattern is present in 2D array. Pattern can be in any way (all ...
4
votes
2answers
1k views

Simple matrix class C++14

I created a simple 4x4 matrix class (column-major). I would like it to be efficient and to use C++14's full capabilities. Can I improve it? ...
5
votes
2answers
341 views

Printing an N*N array in the rectangular/circular/spiral fashion

The problem is to print an N*N array and fill it with numbers(starting from 1 at (0,0)) in the rectangular/circular/spiral fashion, going inwards, ending at the N*N value. Also find and count the ...
4
votes
2answers
682 views

If zero found in Matrix. make entire row & column zero

Problem is - If in an MxN matrix, a zero is encountered, the entire row and entire column should be set to zero. Example - 8 4 2 2 3 7 1 0 1 9 8 4 ...
7
votes
1answer
552 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()) ...
4
votes
1answer
806 views

Speeding up OpenCL matrix-vector multiplication

So I'd like to get a performance boost above and beyond standard Julia matrix-vector multiply, using my Intel HD Graphics 4000 1536 MB GPU, but I can't do better than an order of magnitude worse ...
3
votes
3answers
245 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 ...
9
votes
2answers
292 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)\...
2
votes
1answer
103 views

Profit sensitivity analysis in base R

Using base R, I've conducted a simple profit sensitivity analysis, i.e. measuring the impact on profit if there is a change in price, variable cost per sale, unit sales or fixed costs assuming that ...
3
votes
1answer
331 views

Pretty print a 2D matrix in C (numpy style)

Works for any 2D matrix represented by a 1D array. Comments and criticism welcome. Also wondering whether the code can be made shorter. It's really messy right now. ...
1
vote
1answer
128 views

Horizontally concatenating the entries of two matrices, and padding with NaNs

I have two matrices a and b that have, on each line, some values (null or not) and then a bunch of zeros at the end. I want to ...
5
votes
2answers
91 views

Extract the largest value for each day from a matrix

I have a matrix in which the right-most elements are repeated YYYYMMDD dates in descending order, for example: ...
3
votes
1answer
87 views

Predict new ratings for each user based on their pearson correlation with other users

I am new to R and programming. I have a set of ratings for 45000 users and 40 odd movies. I need to predict new ratings for each user based on their pearson correlation with other users. I also need ...
2
votes
1answer
191 views

Counting matrix elements that have “4” or “5” as a neighbor

I have a matrix (500x500) of integers. For each entry, I need to look at its surrounding neighbours (so 8 elements) and determine what the integers are, and run a function on these integers. Here ...
0
votes
1answer
49 views

Assign values to a matrix based on the location of non-zero elements in another matrix

I have a matrix, A containing ones and zeros. I want to create a new matrix B where the non-zero elements are in the same ...
1
vote
1answer
121 views

Merging two square matrices

I would like to optimize a snippet of code, which uses a multiple nested if statements. I am looking for a formula to make it as a combination of multiple arrays ...
5
votes
1answer
2k views

Java method to make a string representation of a matrix

I have coded this static method for converting a double matrix into a nifty String. It considers the width of the matrix to be ...
2
votes
1answer
370 views

Print matrix in spiral order

I have a task formulated like this Please print a matrix in spiral order, clockwise from outer rings to inner rings. I decided to implement it in Clojure. ...
3
votes
1answer
78 views

Symmetry analysis for atom arrangements in a crystal

For a while now I've been meaning to post some of my Haskell code here so that someone can tell me what parts of the language/base library I've been completely overlooking. This is the first thing I'...
2
votes
0answers
369 views

Find minimum distance in matrix

Challenge URL: http://acm.scu.edu.cn/soj/problem.action?id=3330 Windy has a matrix A with size N*M which only contains 0 or 1. The distance between Axy and Apq is: |x-p| + |y-q| Can you help ...
2
votes
2answers
424 views

Print out a Zig-Zag Matrix in Haskell

My task is to code something that prints out this: This is what I have done so far. ...
1
vote
0answers
126 views

Cuda C Matrix Compression

I am using Cuda to learn and implement a CSR matrix compression algorithm. What can I do better relating to C's best practices? main.c: ...