Tag Info

Hot answers tagged

22

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]


12

You can use HoldForm or Defer with Composition if you are still using Pre V10 versions: MatrixForm[Inner[Composition[Defer, Times], A, A, Composition[Defer, Plus]], TableSpacing -> {3, 3}] MatrixForm[Inner[Times, A, A, Composition[HoldForm, Plus]], TableSpacing -> {3, 3}] MatrixForm[Inner[Times, A, A, Plus], ...


11

Using Composition I can apply RotationTransform, TranslationTransform , ShearingTransform one after the other. Graphics3D[{ Opacity[1] , Red , Arrow[{{0, 0, 0}, {1, 0, 0}}] , Green , Arrow[{{0, 0, 0}, {0, 1, 0}}] , Blue , Arrow[{{0, 0, 0}, {0, 0, 1}}] , Opacity[0.2] , GeometricTransformation[Cuboid[-{1, 1, 1}/4, {1, 1, 1}/4], ...


7

SetAttributes[f, Listable]; f@__ := 0 f[a_, b_] /; b != 0 := a/b First@AbsoluteTiming[f[c, d];] Edit On the same spirit, this is 50% faster: SetAttributes[g, Listable]; g[_, 0] := 0 g[a_, b_] := Divide[a, b] First@AbsoluteTiming[g[c, d];]


6

ans2 = c/(1 - Sign@d + d); // AbsoluteTiming {0.7630000, Null} The above answer only works for specific example in the question i.e. it only handles positive lists and identical mask. For more general cases one can use: n = 1000; (* a and b contain non-positive elements now *) origin := RandomInteger[{-9, 9}, {n, n}] a = origin; b = origin; (* c and ...


6

SparseArray can help, given the size and nature of the mask. It's slightly faster to convert c and d to sparse arrays than to convert a and b. mask = SparseArray@RandomChoice[{0, 0, 1}, {n, n}]; First@AbsoluteTiming[ c = mask a; d = mask b; Quiet[foo2 = Block[{Indeterminate = 0}, SparseArray[c] / SparseArray[d]]] ] (* 0.151565 *) Compare: ...


5

For example, this removes both rows and columns containing 100 m = matrix[[Sequence @@ (Complement[Range@Length@matrix, #] & /@ Transpose@Position[matrix, 100])]]; m // MatrixForm For removing rows containing 100 you could do (among others): matrix /. {___, 100, ___} :> Sequence[] or DeleteCases[matrix, {___, 100, ___}]


5

Another way is to use ListConvolve: neighbors = ListConvolve[{{1, 1, 1}, {1, 1, 1}, {1, 1, 1}}, m, {2, 2}, 0]; neighborCount = ListConvolve[{{1, 1, 1}, {1, 1, 1}, {1, 1, 1}}, ConstantArray[1, Dimensions@m], {2, 2}, 0]; neighbors/neighborCount == MeanFilter[m, 1] True However, for speed it's not so good to go over the entire matrix in order to ...


5

You could try this, which is straightforward: A = {{0.5, 1}, {2, 3}}; MatrixQ[A, IntegerQ] (* False *) Or alternatively - still pretty readable though! ArrayQ[A, _, IntegerQ] (* False *) This has the added bonus of being applicable to other arrays, for example: integerMatrix = RandomInteger[10, {10, 10, 10}]; ArrayDepth@integerMatrix (* 3 *) ...


5

Avoiding exact calculation by using approximated numerical value before calculation speeds things up. I'm also calculating only the first Eigenvectors as pointed pot by @Öskå. Now it takes only 15 milliseconds time AbsoluteTiming[Chop@Eigenvectors[N[m], 1, Quartics -> True]] {0.015600, {{-0.0725514, -0.106358, -0.0986766, -0.110735 [...] }}}


4

You can use matrix[[#, #2 ;; #3]] & @@@ triplets A 20 by 10 example: matrix = RandomInteger[10, {20, 10}]; matrix // TableForm triplets = Flatten/@Transpose[{RandomInteger[{1, 20}, {8}], Sort/@RandomInteger[{1, 10}, {8, 2}]}] {{14, 5, 10}, {11, 4, 10}, {2, 2, 10}, {10, 8, 10}, {12, 3, 9}, {3, 3, 5}, {6, 4, 7}, {5, 5, 10}} matrix[[#, #2 ;; ...


4

Matrix multiplication is built in in Mathematica. Just use the dot for multiplication. Here are two 2x2 matrices a = PauliMatrix[1] b = PauliMatrix[3] (* Out[49]= {{0, 1}, {1, 0}} *) (* Out[50]= {{1, 0}, {0, -1}} *) Here's a product a.b (* Out[53]= {{0, -1}, {1, 0}} *) and here is the product of the same factors in reverse order b.a (* Out[52]= ...


4

RandomVariate[NormalDistribution[], {r, r}] does not give you normalized eigenvectors. To obtain an orthonormal basis, you can first generate a random matrix, and then apply Orthogonalize to it. Following is the correct code. r=4;(*matrix dimension*) dom={1,10};(*domain of random numbers*) eig=DiagonalMatrix[-RandomInteger[dom,r]] (*eigenvalues in diagonal ...


4

The magical words are Singular Value Decomposition. The singular vectors corresponding to small singular values form the kernel. Of course, Singular Value Decomposition is available in Mathematica as SingularValueDecomposition[]. As confirmed by Daniel Lichtblau, the built-in Tolerance option to NullSpace[] does it this exact way.


3

you still can use MapThread. consider the example given by kguler: MapThread[matrix[[#, #2 ;; #3]] &, Transpose[triplets]]


3

there is no solution for the system. sol = Solve[res[[;; 3]] == p[[;; 3]]] (*{{x1 -> -3.16146, x2 -> 20.0611, x3 -> -13.5576}}*) res /. sol (*{{{0.}, {0.}, {0.}, {-3.92175}}}*)


3

Another method is to use Array, the fourth parameter of which sets the function that combines expressions: m = RandomReal[9, {3, 3, 3}]; Array[m[[#]] &, 3, 1, Dot] {{606.041, 638.877, 525.972}, {1011.5, 1068.12, 856.671}, {532.56, 556.236, 435.836}} Equivalent to: Dot @@ m {{606.041, 638.877, 525.972}, {1011.5, 1068.12, 856.671}, {532.56, ...


3

The code as below can achieve the result that you need. Clear@t; mat = RandomInteger[{1, 10}, {20, 4}]; Evaluate[t /@ Range[Length@mat]] = mat Because of (=)Set has the attribute HoldFirst, I use the function Evaluate to evaluate first before proceeding with Set.


3

Perhaps this will work for you. Given a symbol, say t, and a matrix, say m, it will define a set of indexed variables t[1], t[2], ..., t[n], bound to the respective rows of m. SetAttributes[assign, HoldFirst]; assign[name_Symbol, matrix_List] := ( Clear @ name; Do[t[i] = matrix[[i]], {i, 1, Length@matrix}] ) Generate some data. SeedRandom[42]; m ...


3

My favorite method being the one in @belisarius' answer using Part, or a slight variation of it, (matrix[[##&@@Complement@@@Transpose[{Range@Dimensions@matrix, Transpose@Position[matrix, 100]}]]]), here are a few more, clunkier, alternatives: matrix//MatrixForm pattern = Join @@ ({{#, _}, {_, #2}} & @@@ Position[matrix, 100]); m2 = ...


3

You can use the Postion to exact the positions of 100 tagPos=Position[matrix, 100] (*{{3, 2}, {5, 5}, {6, 7}}*) Then using the Last to achieve the column of 100 Rest/@Position[matrix, 100] (*{{2}, {5}, {7}}*) So lastly, Delete res= Delete[Transpose@matrix, Rest /@ tagPos] // Transpose; MatrixForm@res $$ \left( \begin{array}{cccc} 1 & 3 ...


2

exp = (a A + a B) /. {a -> 2, A -> {{1, 2}, {3, 4}}, B -> {{8, 7}, {6, 5}}}; exp[[1, 1]] The original code takes part [[1,1]] of the expression which is a, hence 2. Alternatively you could put parentheses around expression and rules and take part or Part[(a A + a B) /. {a -> 2, A -> {{1, 2}, {3, 4}}, B -> {{8, 7}, {6, 5}}}, 1, ...


2

Although I prefer kguler's method I don't want to be left out of the fun therefore: Cases[triplets, {r_, c1_, c2_} :> matrix[[r, c1 ;; c2]]]


2

Here is one possible way to do it: L1 = {{1, 2}, {1, 3}, {1, 4}}; L2 = {{1, 2}, {1, 3, 2}, {1, 4, 3, 2}}; Outer[Boole[#1 == #2[[{1, -1}]]] &, L1, L2, 1] which produces $$\left( \begin{array}{ccc} 1 & 1 & 1 \\ 0 & 0 & 0 \\ 0 & 0 & 0 \\ \end{array} \right),$$ which shows that the computation of L2 is redundant, since the ...


2

You can generate such matrices using the following code: n = 8; MatrixForm[ Table[Piecewise[{{α, i == j}, {β, Abs[i - j] == 1 || Abs[i - j] == n - 1}}], {i, n}, {j, n}]] which generates $$\left( \begin{array}{cccccccc} \alpha & \beta & 0 & 0 & 0 & 0 & 0 & \beta \\ \beta & \alpha & \beta & 0 & 0 ...


2

If you are going to work with non-commutative algebras like the matrix multiplication I recommend you try the NCAlgebra package. << NC` << NCAlgebra` (2 a) ** (3 b) 6 a ** b P.S. In NCAlgebra all lowercase variables are non-commutative by default.


2

Clear[A, n, k, nn, aa] A = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; Print["A"] MatrixForm[A] aa = Length[A]; Print["First step in matrix multiplication A times A"] MatrixForm[ Table[Flatten[ Table[Table[ StringJoin[{"(", ToString[A[[nn, k]]], ")", "\[CenterDot]", "(", ToString[A[[k, n]]], ")", If[k < aa, "+", If[n == aa, "", ","]]}], ...


2

M = {{1, 3, 3}, {3, 4, 4}, {5, 5, 5}} {{1, 3, 3}, {3, 4, 4}, {5, 5, 5}} P = {{1, 2, 6}, {3, 6, 4}, {5, 9, 5}} {{1, 2, 6}, {3, 6, 4}, {5, 9, 5}} M.P {{25, 47, 33}, {35, 66, 54}, {45, 85, 75}} Or, if you want the nice display be sure not to include that in the definition of your matrix MatrixForm[M = {{1, 3, 3}, {3, 4, 4}, {5, 5, 5}}] ...


2

c/(d /. (0 | 0. -> Infinity));


2

For the given example where no values in b are zero before the mask one can use: Divide[mask*a, b] Note the use of explicit Divide for optimum performance. Timings: (* your data *) (r1 = Quiet[c/d /. Indeterminate -> 0]); // AbsoluteTiming // First (r2 = Divide[mask*a, b]); // AbsoluteTiming // First r1 == r2 4.578262 0.212012 ...



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