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.

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.

Matrices with the same number of rows and columns can be added or subtracted element by element. But the rule for matrix multiplication is that two matrices can be multiplied only when the number of columns in the first equals the number of rows in the second.

Types of matrices

A matrix can be represented as a 2-d (two dimensional) array in any programming language, where one of the two dimensions defines row elements of a matrix, and other dimension defines column elements of the matrix.

Tag usage

The tag can contain programming related problems in implementing matrices. Questions in this tag will be related to a multidimensional array, so these questions can also be tagged with . Please avoid mathematical problems on stackoverflow (use the Mathematics Stack Exchange site instead).

Also note these related tags:

Read more

36680 questions
794
votes
28 answers

How to define a two-dimensional array?

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 = [5][5] Error: Traceback ... IndexError: list index out of range What…
Masoud Abasian
  • 9,309
  • 5
  • 20
  • 22
544
votes
12 answers

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…
Cristian
  • 38,467
  • 25
  • 81
  • 98
382
votes
5 answers

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…
levesque
  • 7,970
  • 10
  • 31
  • 44
375
votes
9 answers

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…
Joe Kearney
  • 6,947
  • 5
  • 32
  • 42
361
votes
7 answers

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)))…
clwen
  • 16,956
  • 27
  • 70
  • 91
326
votes
63 answers

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…
swilliams
  • 44,959
  • 24
  • 94
  • 129
259
votes
11 answers

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

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…
Catskul
  • 15,635
  • 13
  • 75
  • 111
239
votes
1 answer

numpy matrix vector multiplication

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 cannot find any information about how this is done in Python's Numpy…
user3272574
  • 2,557
  • 2
  • 10
  • 14
213
votes
32 answers

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…
abc
  • 2,291
  • 3
  • 23
  • 33
196
votes
12 answers

Why is MATLAB so fast in matrix multiplication?

I am making some benchmarks with CUDA, C++, C#, Java, and using MATLAB for verification and matrix generation. When I perform matrix multiplication with MATLAB, 2048x2048 and even bigger matrices are almost instantly multiplied. …
Wolf
  • 3,007
  • 3
  • 17
  • 10
182
votes
25 answers

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…
ckersch
  • 6,527
  • 2
  • 29
  • 41
169
votes
10 answers

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([[1], [2], [3], [4]]), I want to get A = array([1,2,3,4]). To achieve it, I use A = np.array(M.T)[0]. Does…
yassin
  • 5,881
  • 7
  • 31
  • 38
165
votes
9 answers

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,…
Benjamin
  • 10,449
  • 13
  • 64
  • 112
162
votes
34 answers

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…
Can Berk Güder
  • 99,195
  • 24
  • 125
  • 135
160
votes
6 answers

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: …
peter2108
  • 4,252
  • 5
  • 21
  • 17
1
2 3
99 100