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

40841 questions
175
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
  • 5,580
  • 6
  • 24
  • 18
173
votes
4 answers

How to get element-wise matrix multiplication (Hadamard product) in numpy?

I have two matrices a = np.matrix([[1,2], [3,4]]) b = np.matrix([[5,6], [7,8]]) and I want to get the element-wise product, [[1*5,2*6], [3*7,4*8]], equaling [[5,12], [21,32]] I have tried print(np.dot(a,b)) and print(a*b) but both give the…
Malintha
  • 4,512
  • 9
  • 48
  • 82
164
votes
2 answers

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

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 solution I've found…
Kyle Heuton
  • 9,318
  • 4
  • 40
  • 52
162
votes
7 answers

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
  • 2,139
  • 3
  • 14
  • 17
159
votes
7 answers

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…
Tim
  • 1
  • 141
  • 372
  • 590
156
votes
19 answers

Performance of Java matrix math libraries?

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.,…
dfrankow
  • 20,191
  • 41
  • 152
  • 214
156
votes
3 answers

How can I plot a confusion matrix?

I am using scikit-learn for classification of text documents(22000) to 100 classes. I use scikit-learn's confusion matrix method for computing the confusion matrix. model1 = LogisticRegression() model1 = model1.fit(matrix, labels) pred =…
minks
  • 2,859
  • 4
  • 21
  • 29
147
votes
6 answers

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…
MalteseUnderdog
  • 1,971
  • 5
  • 17
  • 17
146
votes
11 answers

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
  • 2,657
  • 5
  • 35
  • 47
144
votes
8 answers

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…
elexhobby
  • 2,588
  • 5
  • 24
  • 33
132
votes
9 answers

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…
JMP
  • 1,457
  • 2
  • 10
  • 5
131
votes
5 answers

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 <- matrix(c(1:3,7:9,4:6), byrow = T, nc = 3) mat [,1] [,2] [,3] [1,] 1 2 3 [2,] 7 8 9 [3,] 4 5 6 I know max(mat[,2]) will…
Jared
  • 39,513
  • 29
  • 110
  • 145
129
votes
5 answers

Counting the number of non-NaN elements in a numpy ndarray in Python

I need to calculate the number of non-NaN elements in a numpy ndarray matrix. How would one efficiently do this in Python? Here is my simple code for achieving this: import numpy as np def numberOfNonNans(data): count = 0 for i in data: …
jjepsuomi
  • 4,223
  • 8
  • 46
  • 74
116
votes
6 answers

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 <- list() > for (i in 1:10) a[[i]] <- c(i,1:5) > a [[1]] [1] 1 1 2 3 4 5 [[2]] [1] 2 1 2 3 4 5 [[3]] [1] 3 1 2 3 4 5 [[4]] [1] 4 1 2 3 4…
Christopher DuBois
  • 42,350
  • 23
  • 71
  • 93
113
votes
12 answers

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
  • 14,714
  • 27
  • 76
  • 97