New answers tagged

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, ...
Syed's user avatar
  • 38k
0 votes

Finding equal values in a list

Using SequenceCases and Repetead: ...
E. Chan-López's user avatar
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}}...
eldo's user avatar
  • 40.6k
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: ...
Craig Carter's user avatar
  • 3,926
4 votes
Accepted

Solve a non-trivial matrix equation for an unknown matrix

Doing a Cholesky decomposition, gives the answer practically instantaneously: ...
Hans Olo's user avatar
  • 1,507
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 ==...
Daniel Huber's user avatar
  • 45.4k
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: ...
Roman's user avatar
  • 44.9k
1 vote

Change last column of a matrix

Using Lookup: ...
Syed's user avatar
  • 38k
1 vote

Change last column of a matrix

ReplaceAt, introduced with V 13.1, is an ideal candidate for this task: ...
eldo's user avatar
  • 40.6k
2 votes

How can I construct a distance table?

Using GroupBy and Dataset as display function: ...
eldo's user avatar
  • 40.6k
2 votes

How to multiply specific columns in a matrix of data?

Since Version 13.1 there is Threaded: ...
eldo's user avatar
  • 40.6k
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, ...
eldo's user avatar
  • 40.6k
1 vote
Accepted

Gauss elimination with pivoting error

If I make the following smallest possible change to your code: ...
Bill's user avatar
  • 11.5k
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} } *) ...
user1066's user avatar
  • 14.4k
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 ...
bill s's user avatar
  • 68k
3 votes

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

Using ReplacePart with Position: ...
E. Chan-López's user avatar
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}}*)
vindobona's user avatar
  • 671
2 votes

Removing duplicates between sublists

First, you can use Subsets, Intersection and DeleteDuplicates to find all excluded numbers, ...
E. Chan-López's user avatar
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, --...
Vitaliy Kaurov's user avatar
2 votes

Removing duplicates between sublists

MapApply[DeleteElements[#1,Flatten[{##2}]]&]@NestList[RotateLeft, #,Length@#-1]&@list (* {{6,6,4},{},{3,2,3},{-1}} or ...
user1066's user avatar
  • 14.4k
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: ...
Tugrul Temel's user avatar
  • 5,305
4 votes

Removing duplicates between sublists

...
Syed's user avatar
  • 38k
6 votes

Removing duplicates between sublists

uniqueToParentList = MapIndexed[DeleteCases[Alternatives @@ Flatten @ Drop[list, #2]] @ # &]; uniqueToParentList @ list ...
kglr's user avatar
  • 378k
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. ...
ydd's user avatar
  • 2,654
6 votes
Accepted

Removing duplicates between sublists

Something along these lines maybe? MapThread[Cases[#1, Alternatives @@ #2] &, {list, UniqueElements[list]}] Or ...
lericr's user avatar
  • 22.4k
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: ...
ydd's user avatar
  • 2,654
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: ...
kglr's user avatar
  • 378k
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, ...
eldo's user avatar
  • 40.6k
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}}
Syed's user avatar
  • 38k
1 vote

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

Using ReplaceAll: ...
E. Chan-López's user avatar
1 vote

Nonzero element positions of a matrix

Using Position, Condition and Table: ...
E. Chan-López's user avatar
2 votes

Nonzero element positions of a matrix

Using SequencePosition: First /@ SequencePosition[#, {Except[0]}] & /@ m {{1, 2}, {1, 3}}
Syed's user avatar
  • 38k
2 votes

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

Using SubsetMap: (introduced April 16, 2019) ...
Syed's user avatar
  • 38k
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}}
eldo's user avatar
  • 40.6k
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 ...
eldo's user avatar
  • 40.6k
0 votes

Building matrix from relational data

We can also use Merge: ...
eldo's user avatar
  • 40.6k
0 votes

Replace element in array by checking condition in another list

First, you can use GroupBy to construct a rule: ...
E. Chan-López's user avatar
1 vote

Replace element in array by checking condition in another list

Using Lookup: ...
Syed's user avatar
  • 38k
1 vote

Replace element in array by checking condition in another list

...
eldo's user avatar
  • 40.6k
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: ...
eldo's user avatar
  • 40.6k
2 votes

Analyzing football games (pairing matrix)

...
MelaGo's user avatar
  • 6,841
7 votes
Accepted

Analyzing football games (pairing matrix)

You can use GroupBy, Query (with the "add a column" syntax, and ReverseSortBy. ...
Edmund's user avatar
  • 41.5k
4 votes

Analyzing football games (pairing matrix)

Assuming data is stored in a variable called data: ...
Ben Izd's user avatar
  • 9,079
7 votes

Analyzing football games (pairing matrix)

One way to do it: aggResults = GroupBy[data, First -> Last, Total] <|{"Arsenal", "Brighton"} -> 14, {"Arsenal", "...
vindobona's user avatar
  • 671
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 ...
Daniel Huber's user avatar
  • 45.4k
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]" ...
Daniel Huber's user avatar
  • 45.4k
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, ...
Victor Pinto Msc Student's user avatar
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^{\...
Nasser's user avatar
  • 135k
1 vote

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

Using CoefficientArrays: ...
E. Chan-López's user avatar
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 (...
att's user avatar
  • 286

Top 50 recent answers are included