# Questions tagged [matrix]

16270 questions

23

1.7m

Views

### How to define a two-dimensional array in Python

I want to define a two-dimensional array without an initialized length like this: Matrix = [][] but it does not work... I've tried the code below, but it is wrong too: Matrix =  Error: Traceback ... IndexError: list index out of range What is my mistake?
Masoud Abasian

11

118.4k

Views

### Transpose/Unzip Function (inverse of zip)?

I have a list of 2-item tuples and I'd like to convert them to 2 lists where the first contains the first item in each tuple and the second list holds the second item. For example: original = [('a', 1), ('b', 2), ('c', 3), ('d', 4)] # and I want to become... result = (['a', 'b', 'c', 'd'], [1, 2, 3,...
Cristian

9

63k

Views

### How can I index a MATLAB array returned by a function without first assigning it to a local variable?

For example, if I want to read the middle value from magic(5), I can do so like this: M = magic(5); value = M(3,3); to get value == 13. I'd like to be able to do something like one of these: value = magic(5)(3,3); value = (magic(5))(3,3); to dispense with the intermediate variable. However, MATLAB c...
Joe Kearney

30

273.8k

Views

### How do you rotate a two dimensional array?

Inspired by Raymond Chen's post, say you have a 4x4 two dimensional array, write a function that rotates it 90 degrees. Raymond links to a solution in pseudo code, but I'd like to see some real world stuff.     Becomes:   ...

4

103.4k

Views

### What are the differences between numpy arrays and matrices? Which one should I use?

What are the advantages and disadvantages of each? From what I've seen, either one can work as a replacement for the other if need be, so should I bother using both or should I stick to just one of them? Will the style of the program influence my choice? I am doing some machine learning using numpy,...
levesque

11

117k

Views

### What are the most widely used C++ vector/matrix math/linear algebra libraries, and their cost and benefit tradeoffs? [closed]

It seems that many projects slowly come upon a need to do matrix math, and fall into the trap of first building some vector classes and slowly adding in functionality until they get caught building a half-assed custom linear algebra library, and depending on it. I'd like to avoid that while not bui...
Catskul

30

16.2k

Views

### Bomb dropping algorithm

I have an n x m matrix consisting of non-negative integers. For example: 2 3 4 7 1 1 5 2 6 2 4 3 4 2 1 2 1 2 4 1 3 1 3 4 1 2 1 4 3 2 6 9 1 6 4 'Dropping a bomb' decreases by one the number of the target cell and all eight of its neighbours, to a minimum of zero. x x x x X x x x x What is an algorit...

5

84.8k

Views

### Difference between numpy.array shape (R, 1) and (R,)

In numpy, some of the operations return in shape (R, 1) but some return (R,). This will make matrix multiplication more tedious since explicit reshape is required. For example, given a matrix M, if we want to do numpy.dot(M[:,0], numpy.ones((1, R))) where R is the number of rows (of course, the same...
clwen

14

47.1k

Views

### Why is MATLAB so fast in matrix multiplication?

I am making some benchmarks with CUDA, C++, C#, and Java, and using MATLAB for verification and matrix generation. But when I multiply with MATLAB, 2048x2048 and even bigger matrices are almost instantly multiplied. 1024x1024 2048x2048 4096x4096 --------- --------- --------- CUDA C (ms)...
Wolf

3

20.3k

Views

### What is the '@=' symbol for in Python?

I know @ is for decorators, but what is @= for in Python? Is it just reservation for some future idea? This is just one of my many questions while reading tokenizer.py.
Octavia Togami

19

104.5k

Views

### Performance of Java matrix math libraries? [closed]

We are computing something whose runtime is bound by matrix operations. (Some details below if interested.) This experience prompted the following question: Do folk have experience with the performance of Java libraries for matrix math (e.g., multiply, inverse, etc.)? For example: JAMA COLT Apache...

6

79.3k

Views

### Should I use a data.frame or a matrix?

When should one use a data.frame, and when is it better to use a matrix? Both keep data in a rectangular format, so sometimes it's unclear. Are there any general rules of thumb for when to use which data type?
microbe

30

67.9k

Views

### Looping in a spiral

A friend was in need of an algorithm that would let him loop through the elements of an NxM matrix (N and M are odd). I came up with a solution, but I wanted to see if my fellow SO'ers could come up with a better solution. I'm posting my solution as an answer to this question. Example Output: For a...
Can Berk Güder

7

57.8k

Views

### SparseArray vs HashMap

I can think of several reasons why HashMaps with integer keys are much better than SparseArrays: The Android documentation for a SparseArray says 'It is generally slower than a traditional HashMap'. If you write code using HashMaps rather than SparseArrays your code will work with other implementati...
Paul Boddington

10

6.2k

Views

### Why is there huge performance hit in 2048x2048 versus 2047x2047 array multiplication?

I am making some matrix multiplication benchmarking, as previously mentioned in Why is MATLAB so fast in matrix multiplication? Now I've got another issue, when multiplying two 2048x2048 matrices, there is a big difference between C# and others. When I try multiply only 2047x2047 matrices, it seems...
Wolf

7

229.9k

Views

### how does multiplication differ for NumPy Matrix vs Array classes?

The numpy docs recommend using array instead of matrix for working with matrices. However, unlike octave (which I was using till recently), * doesn't perform matrix multiplication, you need to use the function matrixmultipy(). I feel this makes the code very unreadable. Does anybody share my views,...
elexhobby

8

331.9k

Views

### R memory management / cannot allocate vector of size n Mb

I am running into issues trying to use large objects in R. For example: > memory.limit(4000) > a = matrix(NA, 1500000, 60) > a = matrix(NA, 2500000, 60) > a = matrix(NA, 3500000, 60) Error: cannot allocate vector of size 801.1 Mb > a = matrix(NA, 2500000, 60) Error: cannot allocate vector of size 57...
Benjamin

8

165.1k

Views

### Numpy matrix to array

I am using numpy. I have a matrix with 1 column and N rows and I want to get an array from with N elements. For example, if i have M = matrix([, , , ]), I want to get A = array([1,2,3,4]). To achieve it, I use A = np.array(M.T). Does anyone know a more elegant way to get the same resu...
yassin

1

264.9k

Views

### numpy matrix vector multiplication [duplicate]

This question already has an answer here: how does multiplication differ for NumPy Matrix vs Array classes? 7 answers When I multiply two numpy arrays of sizes (n x n)*(n x 1), I get a matrix of size (n x n). Following normal matrix multiplication rules, a (n x 1) vector is expected, but I simply c...
user3272574

3

132k

Views

### Finding row index containing maximum value using R

Given the following matrix lets assume I want to find the maximum value in column two: mat
Jared

5

298.7k

Views

### Select rows of a matrix that meet a condition

In R with a matrix: one two three four [1,] 1 6 11 16 [2,] 2 7 12 17 [3,] 3 8 11 18 [4,] 4 9 11 19 [5,] 5 10 15 20 I want to extract the submatrix whose rows have column three = 11. That is: one two three four [1,] 1 6 11 16 [3,] 3 8 11 1...
peter2108

4

55.6k

Views

### Reshape three column data frame to matrix (“long” to “wide” format)

I have a data.frame that looks like this. x a 1 x b 2 x c 3 y a 3 y b 3 y c 2 I want this in matrix form so I can feed it to heatmap to make a plot. The result should look something like: a b c x 1 2 3 y 3 3 2 I have tried cast from the reshape package and I have tried w...
MalteseUnderdog

17

49.9k

Views

### Transposing a 2D-array in JavaScript

I've got an array of arrays, something like: [ [1,2,3], [1,2,3], [1,2,3], ] I would like to transpose it to get the following array: [ [1,1,1], [2,2,2], [3,3,3], ] It's not difficult to programmatically do so using loops: function transposeArray(array, arrayLength){ var newArray = []; for(var i = 0;...
ckersch

11

105.1k

Views

### What is better, adjacency lists or adjacency matrices for graph problems in C++?

What is better, adjacency lists or adjacency matrix, for graph problems in C++? What are the advantages and disadvantages of each?
magiix

2

364.8k

Views

### How do I find the length (or dimensions, size) of a numpy matrix in python? [duplicate]

This question already has an answer here: Numpy array dimensions 6 answers For a numpy matrix in python from numpy import matrix A = matrix([[1,2],[3,4]]) How can I find the length of a row (or column) of this matrix? Equivalently, how can I know the number of rows or columns? So far, the only solu...
Kyle Heuton

6

183.6k

Views

### Apply a function to every row of a matrix or a data frame

Suppose I have a n by 2 matrix and a function that takes a 2-vector as one of its arguments. I would like to apply the function to each row of the matrix and get a n-vector. How to do this in R? For example, I would like to compute the density of a 2D standard Normal distribution on three points: bi...
Tim

11

86.3k

Views

### How can I apply a function to every row/column of a matrix in MATLAB?

You can apply a function to every item in a vector by saying, for example, v + 1, or you can use the function arrayfun. How can I do it for every row/column of a matrix without using a for loop?
FurtiveFelon

6

100.1k

Views

### How do I make a matrix from a list of vectors in R?

Goal: from a list of vectors of equal length, create a matrix where each vector becomes a row. Example: > a for (i in 1:10) a[[i]] a []  1 1 2 3 4 5 []  2 1 2 3 4 5 []  3 1 2 3 4 5 []  4 1 2 3 4 5 []  5 1 2 3 4 5 []  6 1 2 3 4 5 []  7 1 2 3 4 5 []  8...
Christopher DuBois

4

26.1k

Views

### Are Javascript arrays sparse?

That is, if I use the current time as an index into the array: array[Date.getTime()] = value; will the interpreter instantiate all the elements from 0 to now? Do different browsers do it differently? I remember there used to be a bug in the AIX kernel, which would create pseudo-ttys on request, but...
Berry

7

27.7k

Views

### Why are quaternions used for rotations?

I'm a physicist, and have been learning some programming, and have come across a lot of people using quaternions for rotations instead of writing things in matrix/vector form. In physics, there are very good reasons we don't use quaternions (despite the bizarre story that's occasionally told about...
JMP

8

124.9k

Views

### Android: How to rotate a bitmap on a center point

I've been looking for over a day for a solution to this problem but nothing helps, even the answers here. Documentation doesn't explain anything too. I am simply trying to get a rotation in the direction of another object. The problem is that the bitmap is not rotated around a fixed point, but rathe...
Stefan

10

164.9k

Views

### Convert a matrix to a 1 dimensional array

I have a matrix (32X48). How can I convert the matrix into a single dimensional array?
Alos

4

179k

Views

### Inverse of matrix in R

I was wondering what is your recommended way to compute the inverse of a matrix? The ways I found seem not satisfactory. For example, > c=rbind(c(1, -1/4), c(-1/4, 1)) > c [,1] [,2] [1,] 1.00 -0.25 [2,] -0.25 1.00 > inv(c) Error: could not find function 'inv' > solve(c) [,1]...
Tim

11

77.2k

Views

### Very large matrices using Python and NumPy

NumPy is an extremely useful library, and from using it I've found that it's capable of handling matrices which are quite large (10000 x 10000) easily, but begins to struggle with anything much larger (trying to create a matrix of 50000 x 50000 fails). Obviously, this is because of the massive memor...
Peter

5

189.2k

Views

### How can I create a correlation matrix in R?

I have 92 set of data of same type. I want to make a correlation matrix for any two combination possible. i.e. I want a matrix of 92 x92. such that element (ci,cj) should be correlation between ci and cj. How do I do that?

8

296.1k

Views

### How do I iterate through each element in an n-dimensional matrix in MATLAB?

I have a problem. I need to iterate through every element in an n-dimensional matrix in MATLAB. The problem is, I don't know how to do this for an arbitrary number of dimensions. I know I can say for i = 1:size(m,1) for j = 1:size(m,2) for k = 1:size(m,3) and so on, but is there a way to do it for a...
rlbond

2

7.8k

Views

### Join tiles in Corona SDK into one word for a Breakout game grid?

I have a game project to re-implement Breakout. I want to display two words, each word on a line. They are joined by the bricks block. Inside, the top line is the first name, aligned left. The bottom line is the last name, aligned right. They are input from textboxes, and rendered as shown: Each sec...
nguyentrungkien

3

36.1k

Views

### Difference between numpy dot() and Python 3.5+ matrix multiplication @

I recently moved to Python 3.5 and noticed the new matrix multiplication operator (@) sometimes behaves differently from the numpy dot operator. In example, for 3d arrays: import numpy as np a = np.random.rand(8,13,13) b = np.random.rand(8,13,13) c = a @ b # Python 3.5+ d = np.dot(a, b) The @ opera...
blaz

7

170.7k

Views

### How can I count the number of elements of a given value in a matrix?

Does anyone know how to count the number of times a value appears in a matrix? For example, if I have a 1500 x 1 matrix M (vector) which stores the values of weekdays (1 - 7), how could I count how many Sundays (1), Mondays(2), ... , Saturdays(7) are stored in M?
Niko Gamulin

5