New answers tagged matrix
5
votes
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, ...
0
votes
0
votes
Finding equal values in a list
Using the Query "fork-operator"
Query[All /* Transpose, {First, Length}] @ Split[v]
{{13.51, 12.36, 12.85}, {2, 4, 4}}...
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:
...
4
votes
Accepted
Solve a non-trivial matrix equation for an unknown matrix
Doing a Cholesky decomposition, gives the answer practically instantaneously:
...
1
vote
Solve a non-trivial matrix equation for an unknown matrix
This is only half the answer. I am able to solve for D, but to get B from D seems another problem.
Note, B is symmetric. But then B.Transpose is B.B. And this is also symmetric.
We may solve Dmatrix ==...
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:
...
1
vote
1
vote
Change last column of a matrix
ReplaceAt, introduced with V 13.1, is an ideal candidate for this task:
...
2
votes
2
votes
How to multiply specific columns in a matrix of data?
Since Version 13.1 there is Threaded:
...
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, ...
1
vote
Accepted
Gauss elimination with pivoting error
If I make the following smallest possible change to your code:
...
2
votes
Function to replace values of elements of matrix with 0 unless the value is -1
SparseArray[Position[amat,-1]-> -1, Dimensions@amat]//Normal
(* {
{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,0,-1,0}
} *)
...
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
Function to replace values of elements of matrix with 0 unless the value is -1
Using ReplacePart with Position:
...
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
Removing duplicates between sublists
First, you can use Subsets, Intersection and DeleteDuplicates to find all excluded numbers, ...
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, --...
2
votes
Removing duplicates between sublists
MapApply[DeleteElements[#1,Flatten[{##2}]]&]@NestList[RotateLeft,
#,Length@#-1]&@list
(* {{6,6,4},{},{3,2,3},{-1}}
or
...
1
vote
How to reduce the dimension of a matrix by matrix operation?
Following @Sjoerd Smit's comments, I have the following answer to my question:
...
4
votes
6
votes
Removing duplicates between sublists
uniqueToParentList =
MapIndexed[DeleteCases[Alternatives @@ Flatten @ Drop[list, #2]] @ # &];
uniqueToParentList @ list
...
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.
...
6
votes
Accepted
Removing duplicates between sublists
Something along these lines maybe?
MapThread[Cases[#1, Alternatives @@ #2] &, {list, UniqueElements[list]}]
Or
...
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:
...
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
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, ...
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}}
1
vote
1
vote
2
votes
Nonzero element positions of a matrix
Using SequencePosition:
First /@ SequencePosition[#, {Except[0]}] & /@ m
{{1, 2}, {1, 3}}
2
votes
How to change the first value and last value in a matrix?
Using SubsetMap: (introduced April 16, 2019)
...
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}}
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 ...
0
votes
0
votes
Replace element in array by checking condition in another list
First, you can use GroupBy to construct a rule:
...
1
vote
1
vote
1
vote
How to delete duplicates of certain values in list of lists in a specific column of all sublists?
Since Version 10.1 we can define:
...
2
votes
7
votes
Accepted
Analyzing football games (pairing matrix)
You can use GroupBy, Query (with the "add a column" syntax, and ReverseSortBy.
...
4
votes
Analyzing football games (pairing matrix)
Assuming data is stored in a variable called data:
...
7
votes
Analyzing football games (pairing matrix)
One way to do it:
aggResults = GroupBy[data, First -> Last, Total]
<|{"Arsenal", "Brighton"} -> 14, {"Arsenal", "...
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 ...
2
votes
Accepted
Plot several graphs of a function density distribution using Table
DensityPlot has the Attibute "HoldAll":
Attributes[DensityPlot]
{HoldAll, Protected, ReadProtected}
Therefore, if you want to evaluate "ps[B]" ...
1
vote
How do I get a coefficient matrix from a second order ODE's system?
Looking what you have done aboove in your ask, I did it (turn a 2nd order ODE system in 4 equations 1st order ODE system). But, as I said, I need to comper this 2 situations (2nd order ODE system, ...
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^{\...
1
vote
How do I get a coefficient matrix from a second order ODE's system?
Using CoefficientArrays:
...
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 (...
Top 50 recent answers are included
Related Tags
matrix × 3451linear-algebra × 793
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