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
108
votes
16 answers

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 it to get to: 1 5 12 100.6 12.1 …
Dax Durax
  • 1,607
  • 5
  • 23
  • 31
106
votes
1 answer

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 if I do it from terminal.
Bob
  • 10,741
  • 27
  • 89
  • 143
105
votes
7 answers

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 using R. If I have 200 columns and 100 rows, then I…
user483502
  • 1,051
  • 2
  • 7
  • 3
104
votes
3 answers

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. I am trying to extract individual data frames…
MartinT
  • 1,671
  • 3
  • 14
  • 14
102
votes
1 answer

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, maintain the matrix format, and maintain the row/column…
Suraj
  • 35,905
  • 47
  • 139
  • 250
99
votes
5 answers

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, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15…
Mark Ingram
  • 71,849
  • 51
  • 176
  • 230
99
votes
3 answers

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 relations, all are numbers and are assign to…
Another.Chemist
  • 2,386
  • 3
  • 29
  • 43
98
votes
12 answers

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 the pprint module, but it doesn't seem to do what I want.
houbysoft
  • 32,532
  • 24
  • 103
  • 156
97
votes
4 answers

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
  • 2,135
  • 2
  • 26
  • 31
97
votes
11 answers

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…
Peter
  • 1,771
  • 2
  • 13
  • 8
95
votes
5 answers

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 big as it's width and height but I can't flip it…
Deukalion
  • 2,516
  • 9
  • 32
  • 50
94
votes
11 answers

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 looking for the code to do this. Language ideally…
clamp
  • 33,000
  • 75
  • 203
  • 299
93
votes
16 answers

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 <- matrix(1:10, ncol = 2) tapply(x,…
Joris Meys
  • 106,551
  • 31
  • 221
  • 263
92
votes
4 answers

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 = 5), nrow = 5) # just generate some example…
Tomas
  • 57,621
  • 49
  • 238
  • 373
91
votes
12 answers

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 l r What is the fastest way to do this?
mans
  • 17,104
  • 45
  • 172
  • 321