Hot answers tagged

36 votes

How to check if a vector is an eigenvector of a matrix using mathematica?

You could use MatrixRank. Here is a function that does this: ...
user avatar
  • 124k
34 votes
Accepted

How to show matrix multiplication step by step?

If you have Mathematica 10 you can use the new Inactive functionality ...
user avatar
  • 42.9k
31 votes

How to subtract the column means from each row of a matrix?

It is there: Standardize[mat, Mean, 1 &]
user avatar
  • 133k
30 votes
Accepted

Add a vector to a list of vectors

I recommend using Transpose twice since it is more efficient than other approaches. Moreover Plus has the ...
user avatar
  • 54.4k
29 votes
Accepted

How get eigenvectors without phase jump?

The phase (and length) of the eigenvectors is completely undetermined unless you specify extra conditions in addition to the eigenvalue equation. Given that you don't have any additional conditions, ...
user avatar
  • 95.3k
29 votes

How do you concatenate matrices? And how do you make block matrices?

As Daniel Lichtblau suggested in the comment, ArrayFlatten is the way. ArrayFlatten[{{A1, A2}}] // MatrixForm gives your ...
user avatar
  • 5,069
29 votes
Accepted

Speeding up generation of block diagonal matrix

You are right, it can be done in a fraction of second. One can explicitly construct an array of indexes ...
user avatar
  • 42.9k
28 votes
Accepted

Longest path in 0/1 matrix

As I expressed in my comment above, it is possible (and easy) to use the image processing functions for this. Taking m to be the matrix above the following steps ...
user avatar
  • 87.1k
28 votes
Accepted

Speed up Do loops in MMA 13.0

Here's the fastest I've found: ...
user avatar
  • 220k
26 votes
Accepted

How to speed up `RotationMatrix`?

Having this problem so often, I also generated some tools to handle it which I'd like to share. This is the code (along with a usage message which is basically a small modification of ...
user avatar
25 votes

How to subtract the column means from each row of a matrix?

Well, transposing, subtracting, transposing... Transpose[Transpose[mat] - Mean[mat]]
user avatar
  • 111k
24 votes
Accepted

How to interpret the results of PCA

I generated a multivariate data set to play with called fakedata (see at the bottom of the post). This contains five groups of samples, each in 25-replicate. Each ...
user avatar
  • 61.6k
23 votes
Accepted

How do I create a triangulated surface from points?

It turns out ListSurfacePlot3D does a terribly poor job of approximating the surface in the OP, otherwise one will just apply ...
user avatar
  • 32.5k
23 votes
Accepted

Nearest Kronecker Product

The Pitsianis-Van Loan algorithm turns out to be surprisingly easy to implement in Mathematica: ...
user avatar
23 votes
Accepted

How to subtract the column means from each row of a matrix?

mat - ConstantArray[Mean[mat], 3] or more generally: mat - ConstantArray[Mean[mat], Length[mat]]
user avatar
23 votes

How to roll a graph on the y-axis

We calculate the tangent vector of parametric curve {t^2,t} and the ArcLength from 0 to <...
user avatar
  • 35.9k
22 votes

The magic square function

@Nasser's answer is nice, but slowly when Mod[n,4]==0. Here is a faster code, efficiency is close to Matlab : ...
user avatar
  • 14.2k
22 votes
Accepted

How can I reproduce the result of PrincipalComponents

I vastly prefer the formulation in terms of the singular value decomposition (SVD). This question gets asked a lot by users of these functions, so here's a quick reference: ...
user avatar
22 votes

What are the counterparts in Wolfram to left and right division of a matrix in other programming language, e.g. Julia and MATLAB?

I just stumbled upon it! At least when storing the factorization in a LinearSolveFunction object, we can use it for the transposed solve by supplying a further (not ...
user avatar
22 votes
Accepted

Efficiently defining a SparseArray function

The way the code is written, you can neither exploit packed arrays nor any vectorization. There are two major reasons: Using Rule prevents using packed arrays ...
user avatar
22 votes
Accepted

Can someone boost even more my code?

Exploitation of low rank structure The ordering of summation/dot products is crucial here. As aooiiii pointed out, mat2 has a low-rank tensor product structure. So ...
user avatar
21 votes
Accepted

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

...
user avatar
  • 8,427
21 votes

Replace interior of matrix with zeros

You can do: x[[2 ;; -2, 2 ;; -2]] = 0; x or ...
user avatar
  • 133k
21 votes

Why is calling Dot with many arguments so inefficient?

Here's an edited version of my answer to a related question (elsewhere). Since your central question was about speed (or time complexity), you might wish to know an important result from elementary ...
user avatar
21 votes
Accepted

Sparse Cholesky Decomposition

LinearSolve[] actually computes a permuted Cholesky decomposition; that is, it performs the decomposition $\mathbf P^\top\mathbf A\mathbf P=\mathbf G^\top\mathbf G$....
user avatar
21 votes

What are the counterparts in Wolfram to left and right division of a matrix in other programming language, e.g. Julia and MATLAB?

The documentation for MATLAB's mrdivide states that The operators / and \ are related to each other by the equation B/A = (A'\B')' In light of this we may ...
user avatar
  • 68.9k
21 votes
Accepted

How can I construct this matrix?

a[n_Integer?Positive] := Array[Mod[#2 - #1, n] + 1 &, {n, n}] a[6] // MatrixForm $$ \left( \begin{array}{cccccc} 1 & 2 & 3 & 4 & 5 & 6 \\ ...
user avatar
  • 36.9k
20 votes

eigenvector bug?

This bug has been fixed in V10 mat = {{7/2 - I/2, -1 + I, 1/2 + 5 I/2}, {-1 + I, 5 + I, -1 + I}, {1/2 + 5 I/2, -1 + I, 7/2 - I/2}}; Eigensystem[mat] Gives: ...
user avatar
  • 32.5k
20 votes
Accepted

Analytic determinant of a sparse 25x25 matrix?

Using the properties of Block matrices: $$\det\begin{pmatrix}\mathbf A&\mathbf B\\\mathbf C&\mathbf D\end{pmatrix}=\det(\mathbf A)\det\left(\mathbf D-\mathbf C\mathbf A^{-1}\mathbf B\right)$$ ...
user avatar
20 votes

Get the coefficient matrix from a quadratic form

Here is a very short solution: qf = a x^2 + b y^2 + c z^2 + 2 d x y + 2 e x z + 2 f y z; 1/2 D[qf, {{x, y, z}, 2}] (* ==> {{a, d, e}, {d, b, f}, {e, f, c}} *) ...
user avatar
  • 95.3k

Only top scored, non community-wiki answers of a minimum length are eligible