# Tag Info

Accepted

### Analyzing football games (pairing matrix)

You can use GroupBy, Query (with the "add a column" syntax, and ReverseSortBy. ...

### Analyzing football games (pairing matrix)

One way to do it: aggResults = GroupBy[data, First -> Last, Total] <|{"Arsenal", "Brighton"} -> 14, {"Arsenal", "...

### Restrict sorting to a certain range

You just need to ReverseSort the data and shift the total to the bottom: RotateLeft[ReverseSortBy[data, Last], 1] // Dataset add-on Since I rely on ...
Accepted

### Function to replace values of elements of matrix with 0 unless the value is -1

Using the data of @Syed: ReplaceAt[x_ /; x != -1 :> 0, {All, All}] @ amat {{0, 0, -1, 0, 0, 0, 0}, {0, 0, -1, 0, 0, 0, 0}, {0, -1, 0, 0, 0, 0, 0}, {0, 0, 0, 0, ...

### Function to replace values of elements of matrix with 0 unless the value is -1

Using Syed's input example: SeedRandom; amat = RandomInteger[{-3, 3}, {4, 7}]; 1. You can use Unitize as follows: ...
Accepted

### Removing duplicates between sublists

Something along these lines maybe? MapThread[Cases[#1, Alternatives @@ #2] &, {list, UniqueElements[list]}] Or ...

### Removing duplicates between sublists

uniqueToParentList = MapIndexed[DeleteCases[Alternatives @@ Flatten @ Drop[list, #2]] @ # &]; uniqueToParentList @ list ...

### Function to replace values of elements of matrix with 0 unless the value is -1

We can squish the upper and lower bounds of Clip together at -1 to get the desired output: ...
Accepted

### Sort sequences by their "randomness"

Using Entropy: SortBy[mat, Entropy] {{1, 1, 1, 1, 1}, {2, 2, 2, 2, 2}, {1, 2, 3, 4, 5}, {1, 3, 4, 7, 9}, {1, 3, 5, 7, 9}, {5, ...

### Function to replace values of elements of matrix with 0 unless the value is -1

As an example: SeedRandom; (amat = RandomInteger[{-3, 3}, {4, 7}]) // MatrixForm MatrixForm /@ {amat, amat /. {a_ /; a != -1 -> 0}}
Accepted

### Modeling Infection Spread among Different Age Groups with Contact Matrix

I really enjoy your epidemic series :-) It is always better to decouple the weedy details of a specific model from the key question. Key question should be purified to bare minimum. Perhaps helpful, --...

### Analyzing football games (pairing matrix)

Starting from your "data", we scan every element and use "Sow" to put the points in a list. "Sow" works like "first com, first served". Therefore, to get the ...

### Analyzing football games (pairing matrix)

Assuming data is stored in a variable called data: ...

### Removing duplicates between sublists

Probably much more mathematica-like ways to do this. I think using MemberQ as a test could be slow but I am not sure on this. ...

...
Accepted

### Commutant of set of matrices

Take a random matrix: n = 3; M = RandomInteger[{-10, 10}, {n, n}] (* {{9, 1, 8}, {-7, 8, -3}, {7, 10, -7}} *) Build a matrix of unknowns: ...
Accepted

### Solve a non-trivial matrix equation for an unknown matrix

Doing a Cholesky decomposition, gives the answer practically instantaneously: ...

### How can I get the determinant of an 8x8 matrix in a particular form?

Welcome to Mathematica StackExchange. This answer is a bit tedious, but it works. Change the exponentials to trigs: ...

### How do I get a coefficient matrix from a second order ODE's system?

\begin{align*} x^{\prime\prime} & =\frac{k_{2}}{m_{1}}y-\frac{k_{1}+k_{2}}{m_{1}}x\\ y^{\prime\prime} & =\frac{k_{2}}{m_{2}}x-\frac{k_{3}+k_{2}}{m_{2}}y \end{align*} Let \$x_{1}=x,x_{2}=x^{\...
Accepted

### Restrict sorting to a certain range

At first glance, SubsetMap appears to be perfect for the task. However, since it explicitly acts on a List of the elements (...

### Restrict sorting to a certain range

One approach, calling ReverseSort only on the relevant data: ...

...

### How to change the first value and last value in a matrix?

mat = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}; Since V 12.2 there is ApplyTo (//=): Borrowing ...

### Function to replace values of elements of matrix with 0 unless the value is -1

Using ReplacePart with Position: ...

### Function to replace values of elements of matrix with 0 unless the value is -1

One approach I always fall back on is to create a function that does the basic task: g[x_] := Piecewise[{{-1, x == -1}, {0, x =!= -1}}]; Then the function can be ...

### Removing duplicates between sublists

f = x |-> Select[First@x, FreeQ[Flatten@Rest@x, #] &]; f[RotateLeft[list, #]] & /@ Range[0, Length@list - 1] (*{{6, 6, 4}, {}, {3, 2, 3}, {-1}}*)

### Nonzero element positions of a matrix

Using SequencePosition: First /@ SequencePosition[#, {Except}] & /@ m {{1, 2}, {1, 3}}

### Nonzero element positions of a matrix

m = {{1, 2, 0}, {4, 0, 9}}; Flatten @* Values @* KeyDrop @* PositionIndex /@ m {1, 2}, {1, 3}}