Questions on the manipulation of matrices in Mathematica.

learn more… | top users | synonyms (1)

57
votes
7answers
12k views

Elegant operations on matrix rows and columns

Question The Mathematica tutorial has a section 'Basic Matrix Operations', describing operations like transpose, inverse and determinant. These operations all work on entire matrices. I am missing a ...
40
votes
6answers
3k views

Does Mathematica have advanced indexing?

I have two $M \times K$ arrays $L, T$ where I would like to set all the elements in $L$ to zero whenever the corresponding element of $T$ is greater than 15. The ...
35
votes
3answers
1k views

Tiling a square

I wondered if there was a way to automate the process of finding a way to tile a tile into a square. The idea is to represent the tile with a matrix of $0$s for blank space and $1$s for filled spaces ...
30
votes
6answers
1k views

How to visualize/edit a big matrix as a table?

Is it possible to visualize/edit a big matrix as a table ? I often end up exporting/copying big tables to Excel for seeing them, but I would prefer to stay in Mathematica and have a similar view as in ...
28
votes
8answers
1k views

Longest path in 0/1 matrix

I have a matrix of 0's and 1's forming a number of disjoint paths:   I would like to find the lengths of the paths, and from that "spectrum," the longest length (in the above example: 27, ...
24
votes
5answers
3k views

What is the most efficient way to add rows and columns to a matrix?

Say I have a matrix m and a vector v. ...
22
votes
2answers
334 views

eigenvector bug?

I have a fairly simple $3\times3$ complex matrix, $$ M=\left( \begin{array}{ccc} \frac{7}{2}-\frac{i}{2} & -1+i & \frac{1}{2}+\frac{5 i}{2} \\ -1+i & 5+i & -1+i \\ ...
21
votes
2answers
2k views

How to form a block-diagonal Matrix from a list of matrices?

like this: I know join[...] works, but it is a bit troublesome for multiple matrices.I tried DiagonalMatrix[...], but DiagonalMatrix can only form matrix from a list of elements.
21
votes
1answer
1k views

Is there a way to convert an image into a Graph?

I'm trying to convert an image with several overlapping dots into a Graph. The goal is to be able to derive the Kirchhoff matrix for the randomly created "network ...
20
votes
2answers
1k views

Cycles of length N in a graph

If I have an undirected graph represented with an adjacency matrix, how can I find all the subgraphs which are a cycle of length N? I don't really know the math nor the programming language well, so ...
19
votes
4answers
2k views

Why does MatrixForm affect calculations?

This is a really newbie question, but it has me confused. Why does this code work without // MatrixForm and doesn't work with ...
18
votes
2answers
3k views

How to enter matrices in block matrix format?

Example: I have a matrix $R = \left( \begin{array}{cc} A & \mathbf{t} \\ 0 & 1 \end{array} \right) $ where $A$ is 3-by-3 and $\mathbf{t}$ is 3 by 1. Or in Mathematica ...
18
votes
3answers
2k views

How to symbolically do matrix “Block Inversion”?

Consider a block (partitioned) matrix matrix = ArrayFlatten[{{a, b}, {c, d}}] where, a, ...
16
votes
5answers
598 views

How do I generate the upper triangular indices from a list?

I have some list {1,2,3}. How do I generate nested pairs such that I get {{1,2},{1,3},{2,3}}? That is I'd like a way to ...
16
votes
2answers
770 views

Using the Krylov method for Solve: Speeding up a SparseArray calculation

I'm trying to implement this Total Variation Regularized Numerical Differentiation (TVDiff) code in MMA (which I found through this SO answer): essentially I want to differentiate noisy data. The full ...
16
votes
3answers
348 views

How to improve this code for exploring large matrices?

What I'm actually trying to do is to elaborate this question. I'd like to use Pane with scrollbars enabled to explore large (sparse) matrices like this one: Here ...
15
votes
11answers
1k views

Efficient way to build a certain quadratic matrix

For odd n I'm looking for a short and swift way to construct with (f.e.) n = 3 n = 11 ...
15
votes
4answers
1k views

What is the right way to rotate an array?

Suppose I have an array, not necessarily square: a = $\left( \begin{array}{ccc} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{array} \right)$ I want to rotate it like one ...
14
votes
6answers
677 views

How to find the distance of two lists?

I have two lists as the followings: ab = {1, -1, -1, -1, 1, 1}; ac = {1, -1, -1, 1, 1, 1}; How I can find the difference (more precisely, the edit distance) ...
14
votes
6answers
568 views

Matrix Rotation

If I have a matrix of any size, say $\begin{pmatrix} 72 & 32 & 64 \\ 18 & 8 & 16 \\ 63 & 28 & 56 \\ \end{pmatrix}$ $\begin{pmatrix} 72 & 32 \\ 18 & 8 \\ 63 ...
14
votes
3answers
778 views

Constructing a symbolic Hermitian matrix

I need to construct a symbolic Hermitian matrix like m = { { n, a, b, b}, {Conjugate[a], n, b, b}, ... } but I am not able to set ...
14
votes
1answer
302 views

Making Monte Carlo simulation with ParallelTable

I would like to to MonteCarlo simulations of various regressions - basically, doing OLS under various conditions to test approaches. On my macpro 2x 2.8 GHz quad core Xeon, 32 Gb RAM DDR2 @ 667 Mhz, ...
13
votes
5answers
437 views

Graph from binary matrix (not adjacency) respecting the original matrix positions

I have a binary matrix. My objective is to connect every close (without "jump") elements with value equal to 1, but I must keep the point positions, just like it's shown at the image. Can somebody ...
13
votes
3answers
1k views

Mathematica for linear algebra course?

I'm taking a linear algebra / matrix theory course and we are free to use any software we want, and will be "expected to use MATLAB or an equivalent" for homework. The professor and textbook (Applied ...
13
votes
1answer
232 views

Unpacked eigenvectors of complex matrices

I want to calculate eigenvectors of a Hermitian matrix. For example ...
13
votes
1answer
390 views

Block Matrix Algebra with Mathematica

I have come up with some BlockMatrix Algebra for Mathematica to make notations easier. I have the following: ...
12
votes
6answers
601 views

Elegantly split a matrix into positive and negative parts?

I have a matrix $M$ of real components, and I want to split it into two matrices $M^+$ and $M^-$ of the same dimensions as $M$, where $M^+$ contains the positive components of $M$ (the remaining ...
12
votes
4answers
317 views

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 matrix: ...
12
votes
4answers
407 views

How do you decompose a polynomial matrix into its matrix coefficients?

Let's say I have a matrix, $\mathbf{M}$, that is polynomially dependent on a single variable, such as M = {{15 + a^2, a + 5 a^2}, {a - 5 a^2, 2}} and I want to ...
12
votes
4answers
734 views

What is the fastest way to locate an image inside a larger image?

let b = let c = How to do: find[c,b] that returns the bounding box of c in b? ...
12
votes
1answer
1k views

Eigenvalues and Determinant of a large matrix

Can anybody kindly explain to me what is going wrong regarding a simple problem I have? I can find the eigenvalues of a large matrix using Eigenvalues[], but when I ...
12
votes
1answer
445 views

How to extract and replace submatrix of a doubly-periodic matrix?

How can I effectively extract and replace a rectangular subpart of a toroidal matrix, i.e. one where boundaries are connected at opposite ends? Since the submatrix can overhang the edges, a simple ...
11
votes
3answers
792 views

How can I visualize 6 square matrices as a cube?

I have six square matrices of the same dimensions in a list, for example: ...
10
votes
3answers
2k views

Solving a time-dependent Schroedinger equation

I want to solve the time-dependent Schroedinger equation: $$ i\partial_t \psi(t) = H(t)\psi(t) $$ for matrix, time-dependent $H(t)$ and vector $\psi$. What is an efficient way of doing this so ...
10
votes
3answers
920 views

Correcting a correlation matrix to be positive semidefinite

Does Mathematica have a way to "fix" a correlation matrix that is not positive semi-definite? I looked through the documentation and search the internet but could not find anything.
10
votes
5answers
2k views

Best way to create symmetric matrices

From time to time I need to generate symmetric matrices with relatively expensive cost of element evaluation. Most frequently these are Gram matrices where elements are $L_2$ dot products. Here are ...
10
votes
3answers
406 views

Can (compiled) matrix permanent evaluation be further sped-up?

Update  Multiple optimizations that were suggested by members "ssch" and Simon Woods have in aggregate yielded a ~5X code-speedup; and these optimizations now are incorporated in the example ...
10
votes
2answers
872 views

Non-commutative symbolic linear algebra

I am fairly new to Mathematica but I thought I would be a helpful tool to carry out a few simple linear algebra calculations. It seems like an easy task but I cannot figure out how to do it. For ...
10
votes
3answers
325 views

Zero-dimensional matrices

In a recent Mathematica project, $(n \times 0)$- and $(0 \times n)$-dimensional matrices have suddenly become a frustratingly common edge case for me. For instance, consider the following two ...
10
votes
2answers
301 views

Compiling LinearSolve[] or creating a compilable procedural version of it

Earlier today I had a discussion with a representative at Premier Support about the 2 questions I've asked here over the past couple of days: Seeking strategies to deploy a function securely ...
10
votes
2answers
222 views
10
votes
1answer
180 views

Drop selection of columns from a ragged array

I have an application where I need to drop some arbitrary list of columns from a ragged array (where of course the shortest array rows have at least all the specified columns). E.g., given a ragged ...
10
votes
1answer
593 views

Nesting Parallel processes

I just attempted to run code that had nested ParallelMap[] functions. It generates the error message: ParallelMap::subpar: Parallel computations cannot be nested; proceeding with sequential ...
9
votes
4answers
3k views

How can I multiply matrix and vector element wise like Numpy?

I have an $M \times K$ matrix $C$ and a length $K$ vector $v$. I want to form the $M \times K$ matrix $S$ where $s_{ij} = C_{ij}/v_j$. To do this in numpy I simply divide the two objects like this ...
9
votes
8answers
335 views

Fast way to get positions of “boxed” array elements?

Given a 2D array (arbitrary numeric / evaluates to numeric elements, simplified for examples, and can be arbitrary dimensions so long as the operation makes sense, I'm working with 2K X 2K in my ...
9
votes
2answers
1k views

Badly conditioned matrix (General::luc)

With some matrices I am receiving the following message ...
9
votes
2answers
2k views

How to get Matlab data imported with the same dimensions?

I have some Matlab image data with the following dimensions (output is from Matlab): >> size(im) ans = 86 86 3 45 The data imports just fine ...
9
votes
2answers
767 views

How to transform a 3D image by an affine transformation matrix

I have a question concerning Image Processing: I have a stack of images, which I can compose to a 3D image using Image3D. Additionally I have a 4x4 affine transformation matrix. I would like to ...
9
votes
2answers
296 views

Speed up 4D matrix/array generation

I have to fill a 4D array, whose entries are $\mathrm{sinc}\left[j(a-b)^2+j(c-d)^2-\phi\right]$ for a fixed value of $\phi$ (normally -15) and a fixed value of $j$ (normally about 0.00005). The way ...
9
votes
1answer
155 views

Sporadic bugs in multidimensional SparseArray objects

Resolution WRI affirms that SparseArray[__] is broken (in 9.0 and 9.01) WRI's suggested workaround is SparseArray//Normal My ...