# Tag Info

35

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

33

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

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

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

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

24

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

23

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

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

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

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

20

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

19

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

18

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 ... 18 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 ... 17 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]" ... 17 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 ... 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 ... 16 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, ... 16 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 ... 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]:= *) ... 15 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 15 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 ... 15 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 ... 15 diagF = With[{dims = Total@(Dimensions /@ {##})}, SparseArray[Band[{1, 1}, dims] -> {##}, dims]] &; Edit: Much more elegant form (thanks to Mr.Wizard) diagF = SparseArray[Band[{1, 1}] -> {##}] & Example: 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}}; ... 15 For Integer data we also could write: Tr @ Unitize @ BitXor[ab, ac] 1 For Real data we can use the slightly slower but also shorter: Tr @ Unitize[ab - ac] Blackbird challenged me to provide a method that works on all input types. My approach is to select between methods depending on data. diff[a__?(VectorQ[#, IntegerQ] &)] := Tr @ Unitize ... 14 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}}] ... 14 Based upon your update, you are trying to solve the system $$\mathbf{A}\vec{x} = \vec{b}$$ for$\vec{x}$, so LinearSolve is exactly what you want. Also, it has the exact form LinearSolve[A, b] that you're asking for. Internally it uses a form of Gaussian elimination to solve such systems; this is most likely a variant of LU decomposition, but other ... 14 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 = ... 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

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

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