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
609 votes
14 answers
209k 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', ...
Cristian's user avatar
  • 43.3k
450 votes
7 answers
208k 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 ...
levesque's user avatar
  • 8,886
432 votes
8 answers
237k 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 ...
clwen's user avatar
  • 20.4k
401 votes
9 answers
82k 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 =...
Joe Kearney's user avatar
  • 7,437
367 votes
64 answers
408k 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 ...
293 votes
11 answers
231k 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 ...
Catskul's user avatar
  • 18.5k
288 votes
1 answer
787k views

numpy matrix vector multiplication [duplicate]

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, an (n x 1) vector is expected, but I simply cannot find any ...
user3272574's user avatar
  • 3,047
254 votes
25 answers
189k 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 ...
ckersch's user avatar
  • 7,607
214 votes
32 answers
22k 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 ...
207 votes
12 answers
63k views

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 ...
Wolf's user avatar
  • 3,137
197 votes
10 answers
370k 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([[1], [2], [3], [4]]), I want to get A = array([1,2,3,4])....
yassin's user avatar
  • 6,637
196 votes
9 answers
666k 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) ...
Benjamin's user avatar
  • 11.7k
193 votes
10 answers
590k views

Convert a 1D array to a 2D array in numpy

I want to convert a 1-dimensional array into a 2-dimensional array by specifying the number of columns in the 2D array. Something that would work like this: > import numpy as np > A = np.array([...
digbyterrell's user avatar
  • 3,549
188 votes
4 answers
331k views

numpy get index where value is true

>>> ex=np.arange(30) >>> e=np.reshape(ex,[3,10]) >>> e array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [10, 11, 12, 13, 14, 15, 16, 17, 18, 19], [20, 21, 22, ...
change's user avatar
  • 3,488
180 votes
5 answers
475k views

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]], which equals matrix([[5, 12], [21, 32]]) I have ...
Malintha's user avatar
  • 4,662
179 votes
35 answers
109k 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 ...
Can Berk Güder's user avatar
177 votes
6 answers
531k 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 ...
peter2108's user avatar
  • 5,840
166 votes
7 answers
98k 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 ...
microbe's user avatar
  • 2,179
165 votes
2 answers
635k views

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

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 ...
Kyle Heuton's user avatar
  • 9,468
163 votes
7 answers
320k 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 ...
Tim's user avatar
  • 97.1k
162 votes
3 answers
450k views

How can I plot a confusion matrix? [duplicate]

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() ...
minks's user avatar
  • 2,949
156 votes
19 answers
118k 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 ...
150 votes
6 answers
112k views

Reshape three column data frame to matrix ("long" to "wide" format) [duplicate]

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: ...
MalteseUnderdog's user avatar
149 votes
11 answers
282k 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's user avatar
  • 2,697
145 votes
8 answers
250k 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 ...
elexhobby's user avatar
  • 2,638
140 votes
9 answers
58k 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 ...
JMP's user avatar
  • 1,537
133 votes
6 answers
206k views

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 ...
jjepsuomi's user avatar
  • 4,313
132 votes
5 answers
169k 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 <- matrix(c(1:3,7:9,4:6), byrow = T, nc = 3) mat [,1] [,2] [,3] [1,] 1 2 3 [2,] 7 8 ...
Jared's user avatar
  • 39.7k
119 votes
7 answers
121k 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 <- list() > for (i in 1:10) a[[i]] <- c(i,1:5) > a [[1]] [1] 1 1 2 3 4 5 [[...
Christopher DuBois's user avatar
114 votes
12 answers
118k 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's user avatar
109 votes
17 answers
335k views

java Arrays.sort 2d array

I am looking to sort the following array based on the values of [][0] double[][] myArr = new double[mySize][2]; so for example, myArr contents is: 1 5 13 1.55 12 100.6 12.1 .85 I want ...
Dax Durax's user avatar
  • 1,627
107 votes
7 answers
469k views

Sum rows in data.frame or matrix

I have a very large dataframe with rows as observations and columns as genetic markers. I would like to create a new column that contains the sum of a select number of columns for each observation ...
user483502's user avatar
  • 1,071
107 votes
4 answers
150k views

Split a large dataframe into a list of data frames based on common value in column

I have a data frame with 10 columns, collecting actions of "users", where one of the columns contains an ID (not unique, identifying user)(column 10). the length of the data frame is about 750000 rows....
MartinT's user avatar
  • 1,711
107 votes
1 answer
37k views

How to subset matrix to one column, maintain matrix data type, maintain row/column names?

When I subset a matrix to a single column, the result is of class numeric, not matrix (i.e. myMatrix[ , 5 ] to subset to the fifth column). Is there a compact way to subset to a single column, ...
Suraj's user avatar
  • 36k
106 votes
3 answers
172k views

undefined reference to `std::ios_base::Init::Init()'

I write this code to read 3 files, TM is the size of square matrix, LER the No. of rows of an array and from last value define a non-square matrix of (ler/2)*2 Then... the code read a file with some ...
Another.Chemist's user avatar
106 votes
1 answer
188k views

data type not understood

I'm trying to use a matrix to compute stuff. The code is this import numpy as np # some code mmatrix = np.zeros(nrows, ncols) print mmatrix[0, 0] but I get 'data type not understood', and it works ...
zzzbbx's user avatar
  • 10k
103 votes
5 answers
136k views

Scale and mirror SVG object

How do I most easily first scale an object, say 2 * times it's current size and then flip it vertically and horizontally, or both? As of now, I can either set "scale(2,2)" for it to become 2 times as ...
Deukalion's user avatar
  • 2,616
102 votes
4 answers
68k views

In WebGL what are the differences between an attribute, a uniform, and a varying variable?

Is there an analogy that I can think of when comparing these different types, or how these things work? Also, what does uniforming a matrix mean?
Skorpius's user avatar
  • 2,185
101 votes
5 answers
80k views

Confusion between C++ and OpenGL matrix order (row-major vs column-major)

I'm getting thoroughly confused over matrix definitions. I have a matrix class, which holds a float[16] which I assumed is row-major, based on the following observations: float matrixA[16] = { 0, 1, 2,...
Mark Ingram's user avatar
  • 72.8k
99 votes
12 answers
166k views

Pretty print 2D list?

Is there a simple, built-in way to print a 2D Python list as a 2D matrix? So this: [["A", "B"], ["C", "D"]] would become something like A B C D I found ...
houbysoft's user avatar
  • 32.9k
97 votes
11 answers
122k 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 ...
Peter's user avatar
  • 1,771
95 votes
12 answers
143k views

What is the fastest way to transpose a matrix in C++?

I have a matrix (relatively big) that I need to transpose. For example assume that my matrix is a b c d e f g h i j k l m n o p q r I want the result be as follows: a g m b h n c I o d j p e k q f ...
mans's user avatar
  • 17.6k
94 votes
16 answers
107k views

Convert a matrix to a list of column-vectors

Say you want to convert a matrix to a list, where each element of the list contains one column. list() or as.list() obviously won't work, and until now I use a hack using the behaviour of tapply : x &...
Joris Meys's user avatar
  • 108k
94 votes
11 answers
93k views

Inverting a 4x4 matrix

I am looking for a sample code implementation on how to invert a 4x4 matrix. I know there is Gaussian eleminiation, LU decomposition, etc., but instead of looking at them in detail I am really just ...
clamp's user avatar
  • 33.5k
93 votes
11 answers
99k views

Find out if a matrix is positive definite with NumPy

How can I find out if a matrix is positive definite? My matrix is a NumPy matrix. I was expecting to find any related method in the NumPy library, but I didn't have any success.
Zygimantas Gatelis's user avatar
93 votes
4 answers
128k views

Elegant indexing up to end of vector/matrix

Is it possible in R to say - I want all indices from position i to the end of vector/matrix? Say I want a submatrix from 3rd column onwards. I currently only know this way: A = matrix(rep(1:8, each = ...
Tomas's user avatar
  • 58.5k
92 votes
4 answers
165k views

How do I multiply matrices in PyTorch?

With numpy, I can do a simple matrix multiplication like this: a = numpy.ones((3, 2)) b = numpy.ones((2, 1)) result = a.dot(b) However, this does not work with PyTorch: a = torch.ones((3, 2)) b = ...
timbmg's user avatar
  • 3,280
91 votes
5 answers
226k 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 combinations possible. i.e., I want a matrix of 92x92. such that element (ci,cj) should be correlation between ci ...
Swapnil 'Tux' Takle's user avatar
87 votes
8 answers
354k 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 ...
rlbond's user avatar
  • 66.7k
86 votes
8 answers
134k 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 ...
Stefan's user avatar
  • 861

1
2 3 4 5
824