Tag Info

Hot answers tagged

11

A straightforward and clear solution: f[m_] := Flatten@Table[m[[j, i - j + 1]], {i, Length@m}, {j, i}] f@{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} (* {1, 2, 4, 3, 5, 7} *) A fast compiled version: fc = Compile[{{m, _Integer, 2}}, Module[{n = Length@m, res, k = 0}, res = Array[0 &, Quotient[n (n + 1), 2]]; Do[res[[++k]] = m[[j, i - j + 1]], {i, ...


9

Translated Cleve Moler's magic() function from Matlab code to Mathematica. Grid[Partition[MatrixForm@magic[#] & /@ {3, 4, 5, 6, 7, 8, 9, 10}, 4], Frame -> All, FrameStyle -> LightGray] code: magic[n_Integer /; (n > 0 && n != 2)] := Module[{m, j, k, p, i}, (*Translation of Cleve Moler's magic magic() function to ...


8

Update: here Table is faster and more user-friendly then Array. mat[n_] := LowerTriangularize@Table[2 (1 + Boole[j > 1]) (i - 1) Mod[i + j, 2], {i, n}, {j, n}]; mat[10] // MatrixForm It is fast and the result is packed array mat[1000] // Developer`PackedArrayQ // AbsoluteTiming (* {0.142522, True} *)


8

@Nasser's answer is nice, but slowly when Mod[n,4]==0. Here is a faster code, efficiency is close to Matlab : ClearAll[magic] magic[n_?OddQ] := oddOrderMagicSquare[n]; magic[n_ /; n~Mod~4 == 0] := Module[{J, K1, M}, J = Floor[(Range[n]~Mod~4)/2.0]; K1 = Abs@Outer[Plus, J, -J]~BitXor~1; M = Outer[Plus, Range[1, n^2, n], Range[0, n - 1]]; M ...


8

While the documentation does not specifically say that symbolic Hermitian matrices are not necessarily given orthonormal eigenbases, it does say For approximate numerical matrices m, the eigenvectors are normalized. For exact or symbolic matrices m, the eigenvectors are not normalized. From this, it's reasonable to guess that if Mathematica isn't ...


4

f[n_] := Join @@ (Thread[{Range[#], Range[#, 1, -1]}] & /@ Range[n]); fm[mat_] := Extract[mat, f[Length[mat]]] So, m = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; fm[m] yields:{1, 2, 4, 3, 5, 7}


4

Other solutions are fine, but they use old MatrixConditionNumber as a magic box. However, it has a simple idea. The 2-norm condition number of the matrix $M$ is a ratio $\sigma_{\rm max}/\sigma_{\rm min}$ between the maximum and the minimum singular values. The maximum singular value $\sigma_{\rm max}$ can be estimated by a simple power iteration: ...


4

None of the above are particularly "efficient", if that's your goal. By way of example, mat = Module[{p1 = Range[0, 2 # - 2, 2], p2}, p2 = p1*2; p1[[1 ;; ;; 2]] = 0; p2[[2 ;; ;; 2]] = 0; LowerTriangularize@Transpose[PadRight[{p1}, #, {2 p1, p2}]]] &; mat[200];//Timing (* {0., Null} *) And that's on an old netbook. Compared to ...


4

Here is my version using SparseArray: mat2[n_]:=SparseArray[{{i_,j_}/;And[i>j,Mod[i+j,2]==1]:> If[j==1,2,4](i-1)},{n,n},0.]; MatrixForm@mat2[10]


3

Use Transpose function, it would be like: ListPlot[Transpose[{row,col}]]


3

V = {{176}, {648}}; MatrixForm[Mod[V, 26]]


3

Here is a method that works when eigenvalues do not involve Root objects. Perturb symmetrically, and in such a way that equal eigenvalues become unequal (or enough do that we can get an orthogonal set of eigenvectors). Then take the limit as the perturbation goes to zero. Here I add e to the (1,3) and (3,1) positions. pmat = {{1, -2, e, -2, 0, 0}, {-2, 1, ...


3

Also: L2a = L2 /. {">0" -> (Greater[#, 0] &), "<0" -> (Less[#, 0] &)}; MapThread[Apply, {L2a, L1}] (* {k > 0, 1 + 2 k < 0, 3 + 3 k > 0, 4 k > 0, -4 + 5 k > 0, 6 k < 0} *) Or Apply @@@ Thread[{L2a, L1}] (* {k > 0, 1 + 2 k < 0, 3 + 3 k > 0, 4 k > 0, -4 + 5 k > 0, 6 k < 0} *) List /@ Apply @@@ ...


3

f = Function[m, Flatten[ Diagonal[Reverse[m, {2}], #] & /@ Range[Length[m], 0, -1] ] ] f @ {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} {1, 2, 4, 3, 5, 7}


2

Yet another just for fun: m = {{1,2,3},{4,5,6},{7,8,9}}; m[[-#, # ;; 1 ;; -1]] & ~Array~ Length@m ~Flatten~ {{2, 1}} // Reverse {1, 2, 4, 3, 5, 7}


2

Purely (sic) for @Mr.Wizard's entertainment... ;D f @ m_ := m[[(#+#2-#3)/2,(2-#+#2+#3)/2]]&[#,#2,#2^2]&[#,Round@Sqrt@#]&[2#]& ~Array~ (#(#+1)/2&@Length@m) f @ {{1,2,3},{4,5,6},{7,8,9}} (* {1,2,4,3,5,7} *) m = Range[49] ~Partition~ 7; m // MatrixForm f @ m (* ...


2

How about ClearAll[k] L1 = {{k}, {2 k + 1}, {3 k + 3}, {4 k}, {5 k - 4}, {6 k}}; L2 = {">0", "<0", ">0", ">0", ">0", "<0"}; result = MapThread[{ToExpression@StringJoin[ToString[First@#1], #2]} &, {L1, L2}]


2

Suppose we have a grid like this: x = NestList[RotateRight, {1.5, 2/3, Pi, 9/7`, "!"}, 5]; Grid[x] It looks like this in the Front End: The image above was produced by selecting the Cell bracket and choosing File > Save Selection As.... If we use the Context Menu item Copy As > Plain Text we get: 1.5 2/3 \[Pi] 1.28571 ! ! 1.5 2/3 \[Pi] 1.28571 ...


2

Try this: Delete[L1, {#} & /@ L3] Delete[L2, {#} & /@ L3] (* {{2}, {5}, {0}, {7}, {8}} *) (* {2, 5, 0, 7, 8} *) Have fun!


2

Is this what you need to generate your super tuples one by one ? I think this should work : tuple[n_, m_][i_] := Partition[Partition[IntegerDigits[i - 1, 2, m*n^2], n], n] Test For example when n=2and m=3, you have 2^(m*n^2)=4096 super-tuples, and you call the first one with : tuple[2,3][1] (* {{{0, 0}, {0, 0}}, {{0, 0}, {0, 0}}, {{0, 0}, {0, 0}}} *) ...


1

Try f[x_, y_] = Block[{x, y}, Assuming[x ∈ Reals && y ∈ Reals, Log[Det[V[x, y]\[ConjugateTranspose].V[x, y]]] /. e : Conjugate[EllipticTheta[a_, z_, q_]] :> EllipticTheta[a, Conjugate[z], q] // Simplify ] ]; Chop[N[Q[1, 1]]] (* 2.10739 *)


1

I think this meets the spec: gKirkland[rows_, columns_] := Outer[ Function[{i, j}, If[i <= j, 0, If[Mod[i + j, 2] == 0, 0, 1] If[j == 1, 1/2, 1] (4 (i - 1)) ] ], Range[1, rows], Range[1, columns]] gKirkland[16,16] gives the matrix as shown. gKirkland[1000,1000] evaluates in 3 seconds.


1

Making some suggestions for you code... Do[matrixtransformer[2^(i - 1) - m] = N[1/2^i,20], {i, Floor[Log[2, size]]}]; Note that I've used N[] for the values. Since we are not interested in the exact values using N[] reduces time. And for the second part, you can use MatrixPower[] instead of iterating the multiplication. Its quiet fast. But the big ...


1

There are several ways but the one I like is the following: If L1 and L2 have same length then: index = Complement[Range[Length[L1]], L3]; L1[[index]] (*{{2}, {5}, {0}, {7}, {8}}*) L2[[index]] (*{2, 5, 0, 7, 8}*)


1

It is the first time when I see nested Tuples. However, the output is quite simple: Tuples[Tuples[Tuples[{0,1},n],n],m] produces a list of all possible $m\times n\times n$ arrays with elements $0$ or $1$. To sum up a certain function h we can use the following function count[h_, d_] := Sum @@ Prepend[{#, 0, 1} & /@ Flatten@#, h@#] ...


1

The reason that that the orthogonalization fails is not because there is any problem performing Graham Schmidt. It is because no general symbolic solutions to polynomials beyond 5th order are known and computing eigenvalues of a 5x5 matrix is equivalent to solving a 5th order polynomial. (http://mathworld.wolfram.com/Polynomial.html ) . The original problem ...


1

Extract enables you to prep the indices in advance. entries[A_?SquareMatrixQ] := With[{indices = Flatten[ Table[{i, j - i + 1}, {j, Length[A]}, {i, j}], 1]}, Extract[A, indices]] entries[{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}] (* {1, 2, 4, 3, 5, 7} *)


1

another way. The idea is to take diagonal of each larger matrix. But need to rotate it by 90 degrees each time, hence the Reverse[Transpose@mat. Flatten[(Diagonal@Reverse[Transpose@mat[[1;;#, 1;;#]]])& /@ Range[Length[mat]]] (* {1, 2, 4, 3, 5, 7} *)


1

Use a different format from the start to avoid this complication. L1 = {k, 2 k + 1, 3 k + 3, 4 k, 5 k - 4, 6 k}; L2 = {1, 2, 1, 1, 1, 2}; MapThread[{# > 0, # < 0}[[#2]] &, {L1, L2}] {k > 0, 1 + 2 k < 0, 3 + 3 k > 0, 4 k > 0, -4 + 5 k > 0, 6 k < 0}



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