Tag Info

Hot answers tagged

37

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 ...


36

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_, ...


33

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 ...


29

L=ReplacePart[L, Position[T, i_/;i>15]->0] Go with @Szabolcs answer whenever you can


28

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 ...


28

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 ...


26

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, ...


25

==== 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 ...


25

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 illustrate the idea: img = Image@m; ComponentMeasurements[img, "PerimeterCount"] (* {1 -> 3, 2 -> 27, 3 -> 9, 4 -> 6, 5 -> 15, 6 -> 3, 7 -> 6, 8 -> 3, 9 -> 3, 10 -> 3, ...


22

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 ...


22

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 ...


21

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, ...


21

There is an appropriate metrics: HammingDistance[ab, ac] 1 one could use also (but in general it yields different results since it counts transpositions, deletions etc.) DamerauLevenshteinDistance[ab, ac] 1


21

Clip is usually quite fast: m = RandomReal[{-10^6, 10^6}, {3, 3}]; neg = Clip[m, {-Infinity, 0}] pos = Clip[m, {0, Infinity}] (*{{0., -181286., -442666.}, {0., -233694., -847828.}, {-128249., 0., -540037.}} {{947792., 0., 0.}, {755278., 0., 0.}, {0., 63058.1, 0.}}*) neg + pos == m True


20

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 ...


20

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}}


19

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 ...


18

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]" ...


18

Based on the approach of F'x this is a version aimed rather at large arrays. It should perform reasonably well independent of the array size and lets one edit the given variable directly. Performance suffers only from the maximal number of rows and columns to be shown, which can be controlled with the second argument. I did choose to use the "usual" syntax ...


18

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 ...


18

I think the built-in function ArrayComponents is what you need: vec = {1, 4, 4, 8, 7, 7, 4}; ArrayComponents[vec] (* {1,2,2,3,4,4,2} *) mat = {{1, 4}, {2, 7}, {7, 2}, {9, 4}}; ArrayComponents[mat] (* {{1,2},{3,4},{4,3},{5,2}} *) raggedarray = RandomSample /@ (CharacterRange["a", "z"][[#]] & /@ Range[RandomSample[Range[5]]]) (* ...


17

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 ...


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 ...


17

You can do: x[[2 ;; -2, 2 ;; -2]] = 0; x or ReplacePart[x, {i, j} -> 0 /; And @@ MapThread[Less, {{1, 1}, {i, j}, Dimensions@x}]]


16

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]:= *) ...


16

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 = ...


16

SparseArray[Band[{1, 1}, {12, 12}] -> {-3, 2}, Automatic, 1] // MatrixForm Replace {12, 12} with {2012, 2012} for the full array.


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

matOP = {{0, 1, 0, 0, 1, 0, 1, 0}, {0, 1, 0, 0, 1, 0, 1, 0}, {0, 0, 1, 0, 1, 0, 1, 0}, {0, 0, 0, 1, 0, 0, 1, 0}, {0, 0, 0, 0, 1, 1, 0, 0}, {0, 0, 0, 0, 1, 0, 0, 0}}; $\left( \begin{array}{cccccccc} 0 & 1 & 0 & 0 & 1 & 0 & 1 & 0 \\ 0 & 1 & 0 & 0 & 1 & 0 & 1 & 0 \\ 0 & 0 & 1 ...



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