# Tag Info

58

One convenient way to think of Flatten with the second argument is that it performs something like Transpose for ragged (irregular) lists. Here is a simple example: In[63]:= Flatten[{{1,2,3},{4,5},{6,7},{8,9,10}},{{2},{1}}] Out[63]= {{1,4,6,8},{2,5,7,9},{3,10}} What happens is that elements which constituted level 1 in the original list are now ...

56

A second list argument to Flatten serves two purposes. First, it specifies the order in which indices will be iterated when gathering elements. Second, it describes list flattening in the final result. Let's look at each of these capabilities in turn. Iteration Order Consider the following matrix: $m = Array[Subscript[m, Row[{##}]]&, {4, 3, 2}];$m ...

52

Update: This is now available as the BoolEval package on BitBucket. 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 (...

41

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

39

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

38

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 \end{... 36 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 ... 36 Using Outer is here one of the worst methods, and not just because it computes the distance twice, but because you can't leverage vectorization in this approach. This is actually a common issue and an important point to stress: Outer works pairwise and is unable to utilize the possible vectorized nature of the operation it is performing on an element-by-... 35 L=ReplacePart[L, Position[T, i_/;i>15]->0] Go with @Szabolcs answer whenever you can 34 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, -... 33 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, d}]... 30 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 for,... 30 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 ... 29 ==== 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 ... 28 Unfortunately, this requires a lot more devious trickery than I would have preferred. As noted in the documentation at tutorial/UnconstrainedOptimizationQuasiNewtonMethods, the Hessian is not formed directly in the BFGS method, so we have to recover it from the Cholesky factors. However, all of this is done inside the kernel where we cannot access it using ... 27 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 ... 27 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, ... 26 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]" ... 25 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, it's not surprising that there is no well-defined way to plot the real and imaginary parts of each eigenvector component. A simple condition that makes the ... 24 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 ... 24 If you have Mathematica 10 you can use the new Inactive functionality step1 = MatrixForm[Inner[Inactive[Times], A, A, Inactive[Plus]], TableSpacing -> {3, 3}] step2 = Activate[step1, Times] Activate[step2] 23 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 = ... 23 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 23 Update: Just bumped into this: SparseArraySparseBlockMatrix: bmF = With[{r = MapIndexed[#2[[1]] {1, 1}-># &, #, 1]}, SparseArraySparseBlockMatrix[r]]&; a = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; b = {{1, 2}, {3, 4}}; c = {{x, y, z}, {u, v, w}}; bmF[{a, b, c}] // Normal // MatrixForm Original post: diagF = With[{dims = Total@(Dimensions /@ {#... 23 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 22 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 ... 21 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]:= *) ArrayFlatten[... 21 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}} 21 This might be as good a time as any to distill the collective wisdom of Messrs. Huber, McClure, and Toad R. M. As already mentioned, there is this quantity of great interest to people in the business of solving simultaneous linear equations, called the condition number, and conventionally denoted by the symbol$\kappa\$. This is usually associated with a ...

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