Skip to main content
11 votes

How to quickly make a rectangular array of continous 1 and 0 given the start position of 1

Obligatory bare-metal C answer for ultimate speed: ...
Roman's user avatar
  • 48k
11 votes
Accepted

Apply a function row- and columnwise

If we change dt to: ...
Michael E2's user avatar
  • 237k
11 votes
Accepted

Shading of Dataset area

Dataset[dt, Alignment -> Right, Background -> {{"ita" | "por", "20" | "21" | "22"} -> GrayLevel[.9]}] ...
kglr's user avatar
  • 396k
11 votes
Accepted

The Battleship game: Identify objects within a matrix

NearestNeigborGraph + ConnectedComponents ...
kglr's user avatar
  • 396k
10 votes

How to quickly make a rectangular array of continous 1 and 0 given the start position of 1

This is pretty fast: ...
flinty's user avatar
  • 25.5k
10 votes

How to quickly make a rectangular array of continous 1 and 0 given the start position of 1

Instead of constructing every column separately, note that each column is contained in a vector of n 0's and n 1's, we only need to pick the corresponding part. For ease, we create the matrix row wise ...
Daniel Huber's user avatar
  • 52.7k
10 votes
Accepted

How can I change elements in a matrix to a combination of other elements?

A simple way would be to define a function to transform a single triple: transform[{x_, y_, z_}] := {x + Cos[z/x], y, z - Sin[z/x]} and then map it over your list: ...
lericr's user avatar
  • 29.1k
10 votes

Taxicab Geometry

Mathematica has a large set of graph functions. Here's a 10-by-10 graph of labeled nodes. We can use the graph to find every path that has a length of 9 between two nodes, and to display the paths. <...
creidhne's user avatar
  • 5,225
10 votes

How can I build a symmetric matrix of up to 100?

Look up a Toeplitz matrix: m[n_] := a^(ToeplitzMatrix[n]-1) Alternatively, an explicit construction as a rule-based matrix: ...
Roman's user avatar
  • 48k
9 votes

How to quickly make a rectangular array of continous 1 and 0 given the start position of 1

...
Roman's user avatar
  • 48k
9 votes

How can I change elements in a matrix to a combination of other elements?

...
user1066's user avatar
  • 19.4k
9 votes

Visualizing diagrams needed to compute $\operatorname{Tr}(A^3 (A^T)^3)$

Explanation why some graphs appear more than ones: It depends on the number of possible paths - some graphs have only one possibility, the graph below has four. Functions definitions: I borrowed <...
azerbajdzan's user avatar
  • 18.8k
8 votes

How to quickly make a rectangular array of continous 1 and 0 given the start position of 1

...
Syed's user avatar
  • 55.4k
8 votes
Accepted

How to quickly make a rectangular array of continous 1 and 0 given the start position of 1

Plain C-style loop with Compile: ...
Henrik Schumacher's user avatar
8 votes

How to set to 0 all terms in a matrix which contain a minus

F /. _?Internal`SyntacticNegativeQ -> 0 {{β, x β}, {0, 0}} Also ...
kglr's user avatar
  • 396k
8 votes
Accepted

How to set to 0 all terms in a matrix which contain a minus

Why ReplaceAll[] fails: Since the proposed rules replace any negative by 0, when applied at all levels, we get things like ...
Michael E2's user avatar
  • 237k
8 votes

How can I change elements in a matrix to a combination of other elements?

Equivalent to @lericr`s answer except a pure function is used rather than defining a helper function ...
Bob Hanlon's user avatar
  • 159k
8 votes

How can I expand a matrix and fill in zeros?

See the docs for ArrayFlatten k = ArrayFlatten[{{m, 0}, {0, IdentityMatrix[2]}}]; k // MatrixForm $$\left( \...
LouisB's user avatar
  • 12.6k
8 votes

How can I expand a matrix and fill in zeros?

BlockDiagonalMatrix[{m, IdentityMatrix[2]}] To get a "normal" matrix: ...
lericr's user avatar
  • 29.1k
8 votes
Accepted

Find a certain path through a matrix

Update 2: An alternative method using Fold: ...
kglr's user avatar
  • 396k
8 votes

The Battleship game: Identify objects within a matrix

SparseArray + FindClusters ...
kglr's user avatar
  • 396k
8 votes

Why is Mathematica not simplifying eigenvalues?

The trick is to move the exp inside the sqrt and use the double angle trig rule. A = {{x*Exp[I*y], z}, {z, x*Exp[(-I)*y]}} {v1, v2} = Eigenvalues[A] ...
Nasser's user avatar
  • 145k
8 votes

Taxicab Geometry

Certainly a very wasteful method to generate all possible maths, but it's a start: ...
Henrik Schumacher's user avatar
8 votes

Taxicab Geometry

Let's start with an abstraction: routes = Permutations[{up, up, up, up, right, right, right, right, right}]; We now have 126 paths to get from {2,5} to {7,9} by ...
lericr's user avatar
  • 29.1k
7 votes

Fast pairwise difference

As Henrik has demonstrated in his excellent answer, the good old C compiler interface is still super fast -- the fastest if considering the compiling time. But the new compiler (...
Silvia's user avatar
  • 27.6k
7 votes
Accepted

Elegant way to write a system of ODEs with a Matrix

If you have your system as $x'=Ax+f$ then you can set it up as follows ...
Nasser's user avatar
  • 145k
7 votes
Accepted

Replace a column/row of a matrix under a condition by a random number

Modify -> (Rule) to :> (RuleDelayed).
xzczd's user avatar
  • 66.8k
7 votes

How can I change elements in a matrix to a combination of other elements?

Another way using Cases: ...
E. Chan-López's user avatar
7 votes
Accepted

Why the replacement rule works okay for scalars but not okay for matrices?

Make use of the NonConstants option to D: ...
Carl Woll's user avatar
  • 131k
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
  • 4,536

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