Tag Info

Hot answers tagged

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


14

Here's my take using NestList cm[n_] := NestList[# + 1 &, Join[Range[n/2 + 1], Reverse@Range[n/2]], n - 1] Then cm[11] Here's a FoldList version (just as fast): cmf[n_] := FoldList[#1 + #2 &, Join[Range[n/2 + 1], Reverse@Range[n/2]], ConstantArray[1, n - 1]] The above methods according to the benchmarks posted are already as ...


11

This is pretty straightforward and very easy to follow even for someone who just started learning Mathematica. This has its value when you need to read your code a year later, even if you're an experienced user. n = 11; k = (n + 1)/2; row = k - Abs[k - Range[n]]; Table[row + i, {i, 0, n - 1}] Should be fast enough for most application. Benchmarks The ...


11

Edit: See end of post for latest performance enhancement. f=With[{c = Ceiling[#/2]}, c - 1 + Array[#1 - Abs[c - #2] &, {#, #}]] &; f[5] (* {{1, 2, 3, 2, 1}, {2, 3, 4, 3, 2}, {3, 4, 5, 4, 3}, {4, 5, 6, 5, 4}, {5, 6, 7, 6, 5}} *) Short, sweet, fast. For more speed, f5 = With[{c = Ceiling[#/2]}, Subtract[ ArrayPad[ConstantArray[Range[#, # ...


11

Just another alternative. x - ArrayPad[ArrayPad[x, -1], 1] // MatrixForm


9

n = 11; mid = Ceiling[n/2]; mat = SparseArray[{{i_, j_} /; j > mid, {i_, j_} /; j <= mid}:>{n-j+i,i+j-1}, {n, n}]; MatrixForm@mat


9

b[n_] := (Join[#, Rest@Reverse@#] &@Range[n/2 + 1]) + # & /@ Range[0, n-1] b[11] // MatrixForm Edit Enhanced for some speedup, and curiously enough, it competes well with the fastest answers so far (see @rasher's benchamrk): bs[n_] := With[{k = (Join[#, Rest@Reverse@#] &@Range[n/2 + 1])}, s+Range[0, n-1] /.s-> k]


9

MapThread[Append, {A, B}, 2] // MatrixForm


8

SeedRandom[123]; data = 5 + RandomVariate[BinormalDistribution[.5], 20]; TableForm[data,TableHeadings->{Range@Length@data,{"value1", "value2"}}]//Style[#, 16] & Concordant and discordant pairs: From the documentation on KendallTau Generalizing @eldo's post we can get the concordant and discordant pairs of observations as follows: cdpairsF = ...


8

SeedRandom@0; m = Table[RandomInteger[{0, 34}, {2, 2}], {2}, {5}]; Total[m, 2] (* total all elements down to level 2 *) (* {{208, 182}, {220, 146}} *)


8

just a nonpractical variation: Join[A, Map[List, B, {-1}], 3]


8

Depends on what dimension your final matrix is supposed to have. When I should make a guess, I would say you want this TensorProduct[IdentityMatrix[2], IdentityMatrix[2]] // ArrayFlatten


7

Here is a direct approach w/o the graphics/graph functions. Algorithm: create an array with every '1' changed to a unique number (pad zeros around array to avoid dealing with edge issues ) loop over nonzero entries. If any of the four neighbors (on further thought you may only need to look left & down) swap indices to match - this is a global ...


7

I don't know what would be a general pattern but for this case you can use: {MapThread[Max, #, 2]} & /@ n {{ {{0, t, q, dh}, {0, 1, 0, th}, {1, 0, 1, sh}}}, {{{0, t, q, dh}, {1, 1, 1, th}, {1, 0, 0, sh}}}} Alternatively: List /@ MapThread[Max, n, 3] // MatrixForm $\left( \begin{array}{c} \left( \begin{array}{cccc} ...


7

If you are just asking how to use quaternions for rotation in Mathematica, I hope the following helps. You specify the axis with a unit vector and the angle of rotation. Here is one implementation: Needs["Quaternions`"] qr[vec_, u_, a_] := Module[{qv, qu, r}, qv = ReplacePart[Join[{0}, vec], 0 -> Quaternion]; qu = ReplacePart[Join[{Cos[a/2]}, ...


7

f = Module[{tmp = ConstantArray[0, Dimensions[#1] + {0, 0, 1}]}, tmp[[All, All, ;; -2]] = #1; tmp[[All, All, -1]] = #2; tmp] &; f[a, b] (* {{{1, 2, 0}, {3, 4, 0}}, {{5, 6, 1}, {7, 8, 1}}, {{9, 10, 2}, {11, 12, 2}}}*) All methods posted so far: f1 = MapThread[Append, {#1, #2}, 2] &; f2 = Join[#1, Transpose[{#2}, {3, 1, 2}], 3] ...


7

Not nearly as clean as @rm -rf's solution, but here is another image processing solution. Once again m is taken to be the matrix above. mc=MorphologicalComponents@m; Max@Tally[Join @@ mc][[2 ;;, 2]] (*27*) If one wishes to vizualize the exact path: p1=ArrayPlot[mc /. Rule @@@ Rest[Tally[Join@@mc]], Mesh -> All]; (* paths color-coded by length *) ...


7

n = 11; ArrayPad[ HankelMatrix[Range@n, Range[n, n + Floor[n/2]]][[;; , ;; Ceiling[n/2]]], {{0, 0}, {0, Floor[n/2]}}, "Reflected"] or ArrayPad[ Array[Range[#, # + Floor[n/2]] &, n], {{0, 0}, {0, Floor[n/2]}}, "Reflected"] or Transpose[ Range[n] + # & /@ Join[#, #[[-2 ;; 1 ;; -1]]] &@Range[0, Floor[n/2]] ]


7

Although I believe that Kuba's first method is the best approach here is another: zerofill[a_] := a (1 - BoxMatrix[#/2 - 2, #]) & @ Dimensions @ a Now: Array[Times, {5, 8}] // zerofill // MatrixForm $\left( \begin{array}{cccccccc} 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \\ 2 & 0 & 0 & 0 & 0 & 0 & 0 ...


7

This performs quite well on large matrices, seems to outrun the others I've tested so far: Module[{z = ConstantArray[0, Dimensions@#]}, z[[1, All]] = #[[1, All]]; z[[All, 1]] = #[[All, 1]]; z[[-1, All]] = #[[-1, All]]; z[[All, -1]] = #[[All, -1]]; z] & For really large arrays, it would behoove one to work in the sparse domain, where the ...


6

n = 11; k = Table[i, {i, 1 + #, n + #}] & /@ Range[0, n/2]; (Transpose@Join[Most@k, Reverse@k]) // TableForm or n = 11; Table[j, {i, 1, n}, {j, (n - 1)/2 - Abs[Range[-(n - 1)/2, (n - 1)/2]] + i}] // TableForm


6

Nothing special here, but as there wasn't any solution using Outer, I thought I'd post this: With[{n = 11}, (* adjust n *) Outer[#1 + #2 - 1 &, Range[n], Range[n/2 + 1]~Join~Reverse@Range[n/2]]]


5

Unlike other answers here the combination of Join and Transpose will preserve a fully packed array: << Developer` A = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}, {{9, 10}, {11, 12}}} // ToPackedArray; B = {{0, 0}, {1, 1}, {2, 2}} // ToPackedArray; Join[A, Transpose[{B}, {3, 1, 2}], 3] // PackedArrayQ True This allows superior speed and memory ...


5

Here are some more ways to do what is asked a = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}, {{9, 10}, {11, 12}}}; b = {{0, 0}, {1, 1}, {2, 2}}; MapThread[{Sequence @@ #1, #2} &, {a, b}, 2] MapThread[Flatten[{##}] &, {a, b}, 2] MapThread[Insert[#1, #2, -1] &, {a, b}, 2] MapThread[Join[#1, {#2}] &, {a, b}, 2] and finally, using argument ...


5

The two eigenvalues are degenerate. In general whenever you have degenerate eigenvalues there is arbitrariness in how to select the eigenvectors. You can construct a constant vector from the two degenerate eigenvectors like so: m = Import["https://dl.dropboxusercontent.com/u/63413473/ExampleMatrix.mx"]; {evals, evecs} = Eigensystem[m, -2]; You can check ...


5

There is indeed a generic expression for (the essential part of) your integral which leaves the dimension n open. (We will use n istead of k and s = Sigma^(-1) for the matrix in the exponent). Main idea The main idea is to use the function Sequence (and, of course, delayed assingment). Consider an informal expression of the type Integrate[ ...


5

n = Dimensions[x]; ReplacePart[x, {i_, j_} /;2 <= i <= n[[1]] - 1 && 2 <= j <= n[[2]] - 1 :> 0] another way: MapAt[0 &, x, {2 ;; -2, 2 ;; -2}]


5

This bug has been fixed in V10 mat = {{7/2 - I/2, -1 + I, 1/2 + 5 I/2}, {-1 + I, 5 + I, -1 + I}, {1/2 + 5 I/2, -1 + I, 7/2 - I/2}}; Eigensystem[mat] Gives: {{6, 3 + 3 I, 3 - 3 I}, {{1, -2, 1}, {1, 1, 1}, {-1, 0, 1}}} \begin{array}{ccc} 6 & 3+3 i & 3-3 i \\ \{1,-2,1\} & \left\{1,1,1\right\} & \{-1,0,1\} \\ \end{array}


5

This should do the job: AllPairs[n_?EvenQ] := Flatten[Permutations /@ Subsets[Range[n], {n/2}], 1] /. x_Integer :> Sequence[x, x] AllPairs[4] { {1, 1, 2, 2}, {2, 2, 1, 1}, {1, 1, 3, 3}, {3, 3, 1, 1}, {1, 1, 4, 4}, {4, 4, 1, 1}, {2, 2, 3, 3}, {3, 3, 2, 2}, {2, 2, 4, 4}, {4, 4, 2, 2}, {3, 3, 4, 4}, {4, 4, 3, 3} }


5

The compatibility information at Compatibility/tutorial/LinearAlgebra/MatrixManipulation says These functions were available in previous versions of Mathematica and are now available on the web at library.wolfram.com/infocenter/MathSource/6770: LinearEquationsToMatrices InverseMatrixNorm ConditionNumber You can download the original package ...



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