Skip to main content
32 votes
Accepted

Parallelize Map and ParallelMap

I did a bit of debugging to find the cause of the problem. After I found it, the problem no longer seems as outrageous as it looked at first sight. The root cause of the problem is the following ...
Szabolcs's user avatar
  • 236k
25 votes
Accepted

Metropolis-Hastings Algorithm - Significantly slower than Python

Here a bit of inspiration for you. The following code should be approximately as fast as your Python code. Preparation: ...
Henrik Schumacher's user avatar
17 votes
Accepted

Map vs. Table for index-specific operations on 2D arrays

Many index-specific operations can be implemented via MapIndexed with a level specificaton. Your Power example can be written as:...
Ray Shadow's user avatar
  • 7,886
16 votes
Accepted

Map every second element of a list

Edit Ok, so an incorrect answer producing arrays like: {{"0.", RGBColor["0.00000000000", "0.00000000000", "0.00000000000"]}} received +12 and an accept. Let's ...
Kuba's user avatar
  • 138k
16 votes

Simplify the code

☺lookMaNoLetters☺ = 1 ## & @@@ # & /@ # &; ☺lookMaNoLetters☺ @ mylist {{y1 y2 y3, y3 y4 y5}, {w1 w2 w3, w4 w5 w6}} Further variations: ...
kglr's user avatar
  • 401k
16 votes

Monitoring Apply / @@@ over large lists

You can always transform an expression of the form Apply[f,list,level] to the (almost) equivalent Map[Apply[f],list,level]. You ...
Lukas Lang's user avatar
  • 34.4k
15 votes
Accepted

Determining moving ratios over a list

list = {5040, 1460, 280, 76, 16, 2, 1}; Ratios: 1 / Ratios @ list {252/73, 73/14, 70/...
kglr's user avatar
  • 401k
14 votes
Accepted

Map a two argument function to every element on list

Since you're looking to learn more about functional style, I'll elaborate rather than just provide an answer. We start with our table: ...
lericr's user avatar
  • 34.1k
14 votes
Accepted

How do I map a function pairwise?

BlockMap[Apply[F], data, 2, 1]
lericr's user avatar
  • 34.1k
13 votes
Accepted

How can I create an atomic head or treat some head as atomic during Map?

Following this tutorial, tmp = atomicHead[1, 2, 3]; System`Private`SetNoEntry@tmp Map[g, {0, {tmp}}, {-1}] ...
Alexey Popkov's user avatar
13 votes
Accepted

How to create a matrix of paired values from two matrices?

MapThread[List, {m1, m2}, 2] {{{a, w}, {b, x}}, {{c, y}, {d, z}}} More alternatives that yield the same answer: ...
bmf's user avatar
  • 17k
13 votes
Accepted

How can I "multiply" nested lists?

Times @@@ Tuples @ {alist, blist} {{a r, b s, c t}, {a x, b y, c z}, {d r, e s, f t}, {d x, e y, f z}}
kglr's user avatar
  • 401k
12 votes
Accepted

ParallelMap vs Map performance comparsion

The difference between the parallelized and the serial version is not due to auto-compilation. Auto-compilation will be used on the subkernels too. You can easily check that turning it off there ...
Szabolcs's user avatar
  • 236k
12 votes
Accepted

Unexpected behavior with Map

As noted in comments, the standard ways to evaluate such result is to use Composition or pure function. Using Composition: <...
12 votes
Accepted

Simplify the code

Since you appear to want to multiply the list only when all of its elements are atomic, how about just mylist /. {s__?AtomQ} :> Times[s] It appears to be the ...
Shredderroy's user avatar
  • 5,371
12 votes
Accepted

How can I apply a function only to even-index elements of a list?

I changed D to Dd: MapAt[Dd, A, 2 ;; ;; 2]
lericr's user avatar
  • 34.1k
12 votes

How can I apply a function only to even-index elements of a list?

You can use the new ApplyTo: A = {1, 2, 3, 4, 5}; Dd[P_] := P + 1 A[[2 ;; ;; 2]] //= Dd; A (* {1, 3, 3, 5, 5} *)
Roman's user avatar
  • 49.8k
12 votes
Accepted

Maps between finite sets

alist = {3, 4, 5}; blist = {"a", "b"}; mapfs = Tuples@Outer[Rule, blist, alist] $$\begin{array}{l} \{\text{a}\to 3,\text{b}\to 3\} \\ \{\text{...
Syed's user avatar
  • 59.5k
12 votes

How to create a matrix of paired values from two matrices?

Since particular use of Transpose is shown, let me show the particular use of Flatten: ...
xzczd's user avatar
  • 68.4k
12 votes

Accumulated instance count of each list element

accumulateCounts := Module[{c$}, c$[_] = 0; Map[PreIncrement@*c$]] accumulateCounts @ {m, i, s, s, i, s, s, i, p, p, i} ...
kglr's user avatar
  • 401k
11 votes

Adding dots to an empty region of space

I feel this answer is as sensible as the question. First, binarise the image: m = Import["maz1.jpg"] bin = Opening[ColorNegate@Binarize[m], 6] Find the biggest ...
wxffles's user avatar
  • 14.3k
11 votes
Accepted

List of lists elementwise greater/smaller than

Boole@Positive[a-b] {{0, 1, 0}, {1, 0, 1}, {0, 1, 0}} 1-UnitStep[b-a] {{0, 1, 0}, {1, 0, 1}, {0, 1, 0}}
kglr's user avatar
  • 401k
11 votes

Mapping string into integers

For the specific numbering in OP, you can also use LetterNumber: LetterNumber[l] {{2, 3, 4}, {5, 2}, {1, 2, 4, 5}}
kglr's user avatar
  • 401k
11 votes

What should mapping over a number or symbol produce?

@lericr has given a nice answer, but there is another aspect of the OP's question I would emphasize. My feeling is that there is an implicit assumption in the question. The question states that a list ...
Michael E2's user avatar
  • 245k
10 votes
Accepted

How to make a matrix of derivatives?

See D (search for Hessian): D[f[x, y], {{x, y}, 2}]
Michael E2's user avatar
  • 245k
10 votes
Accepted

Map[Remove[#]] doesn't seem to do anything

As stated in the comments, a is evaluated before being fed into Remove. We can prevent this by using ...
Greg Hurst's user avatar
  • 36.8k
10 votes
Accepted

Combination of list elements

If you have to use Map and #1 + #2 + #3 & literally: ...
kglr's user avatar
  • 401k
10 votes

Map every second element of a list

If we define: rgb[s_] := RGBColor @@ StringReplace[s, Whitespace~~n:NumberString :> ToExpression[n]] then we can write any of the following expressions: <...
WReach's user avatar
  • 69.3k
10 votes
Accepted

Parallelize[MapIndexed[...,Association[...]]] returns broken result

Workaround Wolfram support suggested the following workaround until the issue is resolved: ...
Lukas Lang's user avatar
  • 34.4k
10 votes
Accepted

Iterate MapThread with matrices

You can use Tuples: Tuples[f[{a, b, c}, {a, b, c}]] {f[a, a], f[a, b], f[a, c], f[b, a], f[b, b], f[b, c], f[c, a], f[c,...
kglr's user avatar
  • 401k

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