Tag Info

Hot answers tagged

32

In order to provide a user-friendly way to edit a matrix, I usually do the following: a = RandomReal[Range[0, 1], {5, 5}]; Grid[Array[InputField[Dynamic[a[[#1, #2]]], FieldSize -> 5] &, {5, 5}]] Because Dynamic is used in there, the matrix stored in variable a is automatically modified if you changed any of the numbers in the input fields. And ...


26

I recommend using L UnitStep[15 - T] for good performance. To answer your question about boolean indexing: When you write L[T > 15] = 0 in MATLAB, T > 15 evaluates to a boolean matrix of 0s and 1s, which can be used as a special selector index in assignments, as you showed (I am writing this for those Mathematica users who don't know this ...


25

With some diffidence (because there appears to be a Mathematica bug: see below), I would like to offer an answer in the spirit of the OP's original attempt to solve the problem algebraically. Solution This problem can be formulated as a binary integer linear program. The reformulation represents the square (or more generally, a rectangle as implemented ...


24

In this article the author solves the problem of tiling a rectangle by using pieces taken from a set of polyominoes, which are plane geometric figures formed by joining one or more equal squares edge to edge. For example, these are the pentaminoes, polyominoes formed by joining 5 squares: Of course this problem is more difficult than the one you asked ...


22

You're looking for ArrayFlatten. For your example matrices, R = ArrayFlatten[ {{A, {t}\[Transpose]},{0, 1}} ] (* => {{1, 0, 0, 1}, {0, 0, 1, 1}, {0, -1, 0, 1}, {0, 0, 0, 1}} *) The construct {t}\[Transpose] is necessary for ArrayFlatten to treat t as a column matrix. Then to find $\boldsymbol{R}^{-1}$, you run Inverse[R] (* => {{1, 0, 0, ...


22

Using a little Mathematica pattern matching, I think you can get similar performance as @Szabolcs's answer while having nice Matlab-style syntax: replaceWhere[cond_, selectTrue_, selectFalse_] := With[{evaluatedCondition = evaluateTensorCondition[cond]}, selectTrue*evaluatedCondition + selectFalse*(1 - evaluatedCondition)] replaceWhere[cond_, ...


22

==== Method 1 === Here is a way to get a graph from an image. MorphologicalGraph can get you started. img = Import["http://i.stack.imgur.com/9HXZ5.png"]; g = MorphologicalGraph[img] And here is your KirchhoffMatrix of the graph. Please note that MatrixPlot averages values for the best visual representation, - actual plot would be too detailed to be a ...


21

MatrixForm is a wrapper that pretty-prints your matrices. When you do the following: cov = {{0.02, -0.01}, {-0.01, 0.04}} // MatrixForm you're assigning the prettified matrix to cov (i.e., wrapped inside a MatrixForm). This is not accepted as an input by most functions (perhaps all) that take matrix arguments. What you should be doing to actually assign ...


18

The solution is straightforward: Subsets, specifically Subsets[{1,2,3}, {2}] gives {{1, 2}, {1, 3}, {2, 3}} To generate the lower indices, just Reverse them Reverse /@ Subsets[{1,2,3}, {2}] which gives {{2, 1}, {3, 1}, {3, 2}}


17

It's just a matter of the difficulty inherent in the numerical computation of determinants. Here's what Cleve Moler has to say about determinants and characteristic polynomials in chapter 10 of his book on numerical computing: Like the determinant itself, the characteristic polynomial is useful in theoretical considerations and hand calculations, but ...


16

Mathematica offers a pretty complete set of functionality for linear algebra, and it has improved in recent versions. For example, since version 5, Mathematica has offered the generalised Schur decomposition (also known as the QZ decomposition). This certainly wasn't available in earlier versions. It handles sparse matrices and many other wrinkles. And if ...


16

Try something like matrix = SparseArray[{{i_, i_} /; OddQ[i] -> -3, {i_, i_} /; EvenQ[i] -> 2}, {2012,2012}, 1] // Normal; If you are interested in the formal solution for it determinant, Clear[matrix]; matrix[n_] := SparseArray[{{i_, i_} /; OddQ[i] -> -3, {i_, i_} /; EvenQ[i] -> 2}, {n, n}, 1]; you can start guessing a recursion from ...


16

Time-dependent case in the time-dependent case, $[H(t),H(t')]\neq0$ in general and we need to time-order, ie, the operator taking a state from $t=0$ to $t=\tau$ is $U(0,\tau)=\mathcal{T}\exp(-i\int_0^\tau dt\, H(t))$ with $\mathcal{T}$ the time-ordering operator. In practice we just split the time interval into lots of small pieces (basically using the ...


15

Mathematica does not support this directly. You can do things of this sort using an external package called NCAlgebra. http://math.ucsd.edu/~ncalg/ The relevant documentation may be found at http://math.ucsd.edu/~ncalg/DOWNLOAD2010/DOCUMENTATION/html/NCBIGDOCch4.html#x8-510004.4 In particular have a look at "4.4.8 NCLDUDecomposition[aMatrix, Options]" ...


15

ArrayFlatten is much faster than combination of Join and Transpose: m = RandomVariate[NormalDistribution[], {1000, 1000}]; v = RandomVariate[NormalDistribution[], 1000]; Check that ArrayFlatten gives the same output: (* In[54]:=*) ArrayFlatten[{{Transpose[{v}], m}}] == Transpose[Join[{v}, Transpose[m]]] (* Out[54]= True *) (* In[57]:= *) ...


14

I like to use Part even when I don't want to modify the original matrix. This of course requires making a copy but it keeps syntax more consistent. adding column one to column three: m = Range@12 ~Partition~ 3; m // MatrixForm $\left( \begin{array}{ccc} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \\ 10 & 11 & 12 ...


14

Just for the sake of functional programming here is a solution which is neither in speed nor memory comparable to Subsets but which only uses basic list operations and pattern matching: makePairs[{e_, es__}] := Join[{e, #} & /@ {es}, makePairs[{es}]]; makePairs[{e_}] := {}; It grabs the first element of the list and makes pairs with the other ...


14

a = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; b = {{1, 2}, {3, 4}}; ArrayFlatten[{{a, 0}, {0, b}}] // MatrixForm You can Fold this operation over a list of matrices to get a diagonal: a = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; b = {{1, 2}, {3, 4}}; c = {{1, 2, 3}, {4, 5, 6}}; d = {{1, 2}, {3, 4}, {5, 6}}; Fold[ArrayFlatten[{{#, 0}, {0, #2}}] &, a, {b, c, ...


14

The rank of a matrix is typically determined by performing a Gaussian elimination and is given by the number of non-zero rows. In your second case, the large number $5.4\times 10^{12}$, when eventually used as a pivot, gives a badly conditioned matrix (myM2 is the second matrix in your question): RowReduce[myM2] RowReduce::luc: Result for RowReduce of ...


13

With the following obvious replacements for the two graphs: PetersenGraph[5, 2] --> yourgraph, and CycleGraph[5] --->CycleGraph[n] for general n where yourgraph=AdjacencyGraph[yourAdjacencyMatrix] You can use the first example from the docs on SubGraph section Applications: {g, h} = {PetersenGraph[5, 2], CycleGraph[5]}; Grid[{{g, h}}] ...


13

Matrices in Mathematica are nothing but a specific type of list of lists — specifically, a two dimensional list of lists. * is the short form for the Times function, which threads over lists elementwise, and this is what you'd use if you wanted to take the Hadamard product of two matrices. So when you say A*B, you're actually saying Times[A, B]. . on the ...


13

I would (and do) use Join to add both columns and rows: Join[{v}, m] // MatrixForm Join[List /@ v, m, 2] // MatrixForm On my system -- 8.0.4 Mac 10.6.8 -- Join is faster than ArrayFlatten, although there is not a great deal in it: m = RandomVariate[NormalDistribution[], {1000, 1000}]; v = RandomVariate[NormalDistribution[], 1000]; Do[tmp1 = ...


13

Here is a very simple way to do it: Table[1/i! D[M, {a, i}] /. a -> 0, {i, 0, 3}] (* ==> {{{15, 0}, {0, 2}}, {{0, 1}, {1, 0}}, {{1, 5}, {-5, 0}}, {{0, 0}, {0, 0}}} *) This works even if the entries are not polynomials. If they are, you can replace the arbitrary maximum 3 in the Table index by the degree of the polynomial: Max[Exponent[M, a]] ...


13

lists = RandomInteger[{1, 9}, {4, 5}] {{7, 4, 9, 9, 7}, {4, 2, 5, 5, 2}, {6, 5, 9, 2, 4}, {1, 9, 4, 7, 2}} ArrayPad[lists, {{0, 0}, {1, 0}}] {{0, 7, 4, 9, 9, 7}, {0, 4, 2, 5, 5, 2}, {0, 6, 5, 9, 2, 4}, {0, 1, 9, 4, 7, 2}} There are of course many ways to do this. Since you are interested in learning here are some others, more or less ...


13

array0 = {{72, 32, 64}, {18, 8, 16}, {63, 28, 56}}; array1 = SparseArray[Band[{# - 1 + Length@array0[[#]], #}, Automatic, {-1, 1}] -> array0[[#]] & /@ {1, 2, 3}, {5, 5}]; array1 // MatrixForm Update: Generalizing for arbitrary matrix input: rttF = Function[{mat}, With[{dims = Dimensions[mat]}, SparseArray[Band[{# - 1 + Last@dims, #}, ...


12

Szabolcs's answer using UnitStep is the right one for your specific case. It is also the most efficient by far: see the timing results below. Explicit Map operations and size comparisons do seem to be particularly slow. However there is a Boole function that replicates the Matlab approach of returning 1 and 0 instead of True and False, which might be useful ...


12

We can construct this matrix directly as a SparseArray. This allows some classes of numerical matrices to be stored as packed arrays while being combined with symbolic or exact vectors (or vice versa), so there can be storage and run-time efficiency reasons for using a SparseArray, in addition to the obvious benefit of direct construction. On the other hand, ...


12

By default, Mathematica assumes symbols to be complex. However the elements on the main diagonal of a Hermitian matrix are necessarily real. To force Mathematica to interpret the elements on diagonal of m to be real you could replace them by their real part, i.e. m = {{Re[n], a, b, b}, {Conjugate[a], Re[n], b, b}, {Conjugate[b], Conjugate[b], Re[c], ...


11

The keyboard commands Ctrl+Enter, Ctrl+, and Tab can be used to enter this format. You can also use the menu Insert > Table/Matrix to create a table of specified size with placeholders. See Entering Tables and Matrices. Depending on the meaning of the question, this may have some bearing:


11

Interchanging rows This'll swap rows 1 and 3. Permute[mat, Cycles[{{1, 3}}]] To swap columns, you can convert the permutation to a permutation list, and use mat[[All, permList]] Multiplying rows This'll multiply the 3rd row by 5: MapAt[5 # &, mat, 3] This'll change the matrix permanently: mat[[3]] *= 5



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