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 ...
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:
...
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:...
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 ...
16
votes
Simplify the code
☺lookMaNoLetters☺ = 1 ## & @@@ # & /@ # &;
☺lookMaNoLetters☺ @ mylist
{{y1 y2 y3, y3 y4 y5}, {w1 w2 w3, w4 w5 w6}}
Further variations:
...
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 ...
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/...
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:
...
14
votes
Accepted
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}]
...
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:
...
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}}
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 ...
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:
<...
Community wiki
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 ...
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]
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} *)
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{...
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:
...
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}
...
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 ...
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}}
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}}
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 ...
10
votes
Accepted
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 ...
10
votes
Accepted
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:
<...
10
votes
Accepted
Parallelize[MapIndexed[...,Association[...]]] returns broken result
Workaround
Wolfram support suggested the following workaround until the issue is resolved:
...
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,...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
map × 419list-manipulation × 190
functions × 42
plotting × 35
functional-style × 26
performance-tuning × 23
function-construction × 22
pure-function × 21
parallelization × 20
associations × 20
matrix × 18
replacement × 14
graphics × 11
dataset × 11
string-manipulation × 10
slot × 9
core-language × 8
iterators × 8
mapthread × 8
syntax × 7
equation-solving × 6
filtering × 6
code-review × 6
table × 5
compile × 5