Questions tagged [matrix]

Questions on the manipulation of matrices in Mathematica.

Use this tag for questions that involve any kind of matrix operation where that operation is an inherent part of the question. This includes pattern-matching and extracting (subparts of) matrices, structural manipulation of matrices (reshaping, flattening, rotating) and linear algebra problems like matrix multiplication, transposing, eigenvalue calculation, etc. Generally this tag applies to matrices of any dimension.

Questions specifically dealing with linear algebra should also use . For more general questions dealing with lists (e.g. construction, structural rearrangement, element extraction, etc.) use . For questions about displaying matrices in 2D using typesetting use . For more specific questions related to tensors (e.g. geometric objects that describe linear relations between vectors, scalars, and other tensors) use .

Useful links:

Example questions:

3524 questions
19
votes
1 answer

Extract matrix from a system of linear equations

I have a big system of linear equations $f_i(x_1,...x_n)$ with $n$ variables and $n$ equations. I want to extract the matrix $M$ where $\vec{f}(\vec{x})=M\vec{x}$. For example if I have $$ f_1(x) = x + 2y \\ f_2(x) = 3x-y $$ then I want to get the…
physicsGuy
  • 557
  • 3
  • 9
16
votes
1 answer

How to take transpose of a one dimensional vector?

Why the two followings giving me same output? At least one should give me a row vector and another should give me a column vector. MatrixForm[u = {1 , 1, -1, 1}] MatrixForm[v = {{1}, {1}, {-1}, {1}}] When I am trying to get transpose of u by…
Supriyo
  • 265
  • 1
  • 2
  • 7
15
votes
2 answers

The magic square function

There are some matrices such that the sum of columns, the sum of rows, and the sum of diagonals are the same value. Here is an example: 8 1 6 3 5 7 4 9 2 The sum of rows is 15, and so is sum of columns and the sum of…
Nick
  • 459
  • 4
  • 9
13
votes
5 answers

Is there a built in function to obtain the back diagonal of a matrix?

Given the following matrix: m = Array[Subscript[a, #, #2] &, {4, 4}] how can I find the skew diagonal or anti-diagonal or back diagonal of the matrix (shown in red)
M.R.
  • 31,425
  • 8
  • 90
  • 281
12
votes
4 answers

Creating a table/Matrix during a For loop

I have a For loop (with i as a counter), which calculates different values (y1, y2, y3,...) as a function of the variable x. How can I create a table/matrix which contains i lines, each line containing the variable x_i and the values (y1_i, y2_i,…
Solarboy
  • 121
  • 1
  • 1
  • 3
12
votes
4 answers

Inputting a big structured matrix in Mathematica

Hi this is my first post and this is a problem that has bothered me a lot. Suppose we have the $2012\times2012$ square…
11
votes
1 answer

Extracting the principal component analysis vectors in the original basis

I'm using Mathematica's PrincipalComponents[] to do a principal components analysis on a data set with m data points and n variables (m > n). The command produces the m by $n$ matrix which contains the representation of each of the data in the…
William Kennerly
  • 361
  • 3
  • 11
10
votes
8 answers

Assigning row of values to a lower triangular matrix

I want to assign a linear row of values to the elements of a lower triangular matrix, i.e., if the row of values is {1,2,3,4}, then the matrix (4 X 4) should be: {{1, 0, 0, 0}, {2, 1, 0, 0}, {3, 2, 1, 0}, {4, 3, 2, 1}} I can do this with a…
Jorge
  • 103
  • 3
10
votes
5 answers

How can I define a very large matrix efficiently?

Here's a 7x7 matrix: (matrix = {{1, 1, 1, 1, 1, 1, 1}, {1, -1, 0, 0, 0, 0, 0}, {1, 1, -2, 0, 0, 0, 0}, {1, 1, 1, -3, 0, 0, 0}, {1, 1, 1, 1, -4, 0, 0}, {1, 1, 1, 1, 1, -5, 0}, {1, 1, 1, 1, 1, 1, -6}}) // MatrixForm The matrix…
Allure
  • 625
  • 4
  • 11
9
votes
2 answers

How can I obtain this matrix quickly in Mathematica?

Is there a way of obtaining the matrix matFinal quickly by avoiding the following long procedure? s1 = PauliMatrix[1]; s2 = PauliMatrix[2]; s3 = PauliMatrix[3]; u = {{a^2, 0, 0, 0, 0, 0, 0, a b}, {0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0,…
User101
  • 593
  • 2
  • 5
8
votes
3 answers

How to create a general format of a matrix with specified entries?

I want to create a PNG picture of a matrix where its entries are the following: n, n-1, ..., 1, 2n, 2n-1, ..., n+1, 3n, 3n-1, ..., 2n+1, ... n^2, n^2-1, ..., (n-1)n+1 For example, I was thinking something like the picture below (including…
johnny09
  • 395
  • 2
  • 13
8
votes
3 answers

Efficient method to generate Tridiagonal 50 by 50 Matrix?

I'm looking to generate a tridiagonal 50x50 matrix, ideally without using loops. Suggestions on most efficient code for this?
credwood
  • 81
  • 1
8
votes
4 answers

Finding the position of similar elements in a matrix

I want to find the position of the elements which are equal in a matrix. For example in the case of the following matrix: $m =\left( \begin{array}{cc} a & x \\ x & a \\ \end{array} \right)$ m = {{a, x}, {x, a}}; I want to get these two…
MOON
  • 3,864
  • 23
  • 49
8
votes
5 answers

correct way to map a function on entries down the pages in a 3D matrix?

updated: added another method to the methods I tried below for completion. This is new way of doing it, but most likely not very efficient, did not time it I can only visualize 3D matrices in my mind as a book made up of pages. Each page is a 2D…
Nasser
  • 143,286
  • 11
  • 154
  • 359
8
votes
2 answers

Random orthogonal matrix

Is there a way to generate real random orthogonal matrices in mathematica? If Q is an orthogonal matrix, it's properties are
Jasmine
  • 1,225
  • 3
  • 10
1
2 3
24 25