7
votes
Accepted
Analyzing football games (pairing matrix)
You can use GroupBy, Query (with the "add a column" syntax, and ReverseSortBy.
...
7
votes
Analyzing football games (pairing matrix)
One way to do it:
aggResults = GroupBy[data, First -> Last, Total]
<|{"Arsenal", "Brighton"} -> 14, {"Arsenal", "...
6
votes
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 ...
6
votes
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, ...
6
votes
Function to replace values of elements of matrix with 0 unless the value is -1
Using Syed's input example:
SeedRandom[1];
amat = RandomInteger[{-3, 3}, {4, 7}];
1.
You can use Unitize as follows:
...
6
votes
Accepted
Removing duplicates between sublists
Something along these lines maybe?
MapThread[Cases[#1, Alternatives @@ #2] &, {list, UniqueElements[list]}]
Or
...
6
votes
Removing duplicates between sublists
uniqueToParentList =
MapIndexed[DeleteCases[Alternatives @@ Flatten @ Drop[list, #2]] @ # &];
uniqueToParentList @ list
...
5
votes
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:
...
5
votes
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, ...
4
votes
Function to replace values of elements of matrix with 0 unless the value is -1
As an example:
SeedRandom[1];
(amat = RandomInteger[{-3, 3}, {4, 7}]) // MatrixForm
MatrixForm /@ {amat, amat /. {a_ /; a != -1 -> 0}}
4
votes
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, --...
4
votes
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 ...
4
votes
Analyzing football games (pairing matrix)
Assuming data is stored in a variable called data:
...
4
votes
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.
...
4
votes
4
votes
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:
...
4
votes
Accepted
Solve a non-trivial matrix equation for an unknown matrix
Doing a Cholesky decomposition, gives the answer practically instantaneously:
...
4
votes
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:
...
3
votes
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^{\...
3
votes
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 (...
3
votes
Restrict sorting to a certain range
One approach, calling ReverseSort only on the relevant data:
...
3
votes
3
votes
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 ...
3
votes
Function to replace values of elements of matrix with 0 unless the value is -1
Using ReplacePart with Position:
...
3
votes
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 ...
3
votes
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}}*)
2
votes
Nonzero element positions of a matrix
Using SequencePosition:
First /@ SequencePosition[#, {Except[0]}] & /@ m
{{1, 2}, {1, 3}}
2
votes
Nonzero element positions of a matrix
m = {{1, 2, 0}, {4, 0, 9}};
Flatten @* Values @* KeyDrop[0] @* PositionIndex /@ m
{1, 2}, {1, 3}}
2
votes
Remove Successive Duplicate From A Large List (200k by 4 matrix)
m = {{1, 1, 1, 1}, {2, 2, 0, 2}, {0, 1, 0, 0}, {2, 2, 0, 0}};
Since V 13.1 we have
DeleteAdjacentDuplicates /@ m
{{1}, {2, 0, ...
2
votes
How to multiply specific columns in a matrix of data?
Since Version 13.1 there is Threaded:
...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
matrix × 3452linear-algebra × 794
list-manipulation × 762
performance-tuning × 231
equation-solving × 206
eigenvalues × 180
plotting × 141
functions × 119
sparse-arrays × 115
symbolic × 105
tensors × 96
vector × 93
mathematical-optimization × 78
differential-equations × 77
calculus-and-analysis × 77
numerics × 76
replacement × 74
graphs-and-networks × 69
random × 67
simplifying-expressions × 65
function-construction × 59
physics × 50
programming × 47
complex × 47
probability-or-statistics × 42