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

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

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

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]


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

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

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

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


6

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


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

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

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


4

UPDATE Nice work, OP, with $OutputForms. I did not know about that. Here is my take on a complete solution that takes advantage of that find, and adds input handling with MakeExpression. I can't think of a situation in which this would be superior to InterpretationBox for this problem, but it is helpful in more complex cases. If[ FreeQ[$OutputForms, pm = ...


4

Modifying last example in Manipulate>Neat Examples (also the second example in this answer): Manipulate[ ArrayPlot[Take[data, n, n]], {{data, RandomInteger[{0, 1}, {20, 20}]}, ControlType -> None}, {{n, 5}, 1, 20, 1}, Dynamic[ Panel[Grid[Outer[Checkbox[Dynamic[data[[#1, #2]]], {0, 1}] &, Range[n], Range[n]]]]]]


4

The problem here can arise because of numerical underflow which appears for sufficiently large dimension of the problem. Some numerically very small number multiplies the parameter "a" and therefore "a" does not appear in the "solution". Consider a simple example Define the matrix m (fill it with random numbers, here exponentially distributed) In[263]:= ...


4

This is another approach: pairRiffle[n_?EvenQ] := Riffle[#, #] & /@ (Permutations[Range@n, {n/2}]) pairRiffle[4] (* {{1, 1, 2, 2}, {1, 1, 3, 3}, {1, 1, 4, 4}, {2, 2, 1, 1}, {2, 2, 3, 3}, {2, 2, 4, 4}, {3, 3, 1, 1}, {3, 3, 2, 2}, {3, 3, 4, 4}, {4, 4, 1, 1}, {4, 4, 2, 2}, {4, 4, 3, 3}} *) Edit: in fact I could do without the Riffle and just use ...


4

The LinearAlgebra package has been deprecated since Mathematica 5, and is no longer bundled with Mathematica 9 or newer. You can still download a part of it (which contains the MatrixConditionNumber function) at the URL that jtbandes gave in his answer. First, we need to load the package: (* Be sure to install the above linked package in a "LinearAlgebra" ...


4

The term incidence matrix has caused confusion on this site before, so I think it's time to clear this up. There's no standard, generally agreed upon definition of incidence matrix. It's a loose term for a matrix that describes the relationship (connections) between two different classes of objects. What these objects are can vary. When you see the term ...


4

Clip seems perfectly suited: mat = RandomReal[{0, 10}, {100, 100}]; Manipulate[ MatrixPlot[Clip[mat, {threshold, threshold}, {0, 1}]], {threshold, 0, 10} ] Here we use the form of Clip with three arguments: Clip[mat, {threshold, threshold}, {0, 1}] It takes everything below threshold and sets it to 0, and takes everything above threshold and sets ...


3

<< SymbolicC` << Developer` << CCompilerDriver` << CCodeGenerator` Please don't mind these unnecessary abstractions. type = "mint"; abstractFunctionName = "makeMatr"; mainFunctionName = abstractFunctionName <> "I_T"; argumentSingletonGetterFunctionName[type_String] := StringJoin["MArgument_get", type]; getter = ...


3

If only Max were Listable: listMax = Function[, Max[##], Listable]; Then: List /@ listMax @@@ n // MatrixForm $\left( \begin{array}{c} \left( \begin{array}{cccc} 0 & t & q & \text{dh} \\ 1 & 1 & 1 & \text{th} \\ 1 & 0 & 0 & \text{sh} \end{array} \right) \\ \left( \begin{array}{cccc} 0 & t & q & ...


3

pp = {{rr, {{0, t, q, dh}, {0, 1, 0, th}, {1, 1, 0, sh}}, {{0, t, q, dh}, {1, 0, 0, th}, {1, 0, 0, sh}}}, {kk, {{0, t, q, dh}, {0, 1,0, th}, {1, 0, 0, sh}}, {{0, t, q, dh}, {0, 0, 0, th}, {0, 1, 0,sh}}}}; MapAt[MatrixForm, pp, {{All, All}, {}}]



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