Hot answers tagged

31 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 ...
user avatar
  • 224k
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: ...
user avatar
22 votes
Accepted

Can A Plot Be Overlayed With A Map?

Please see the Utility function section for a concise summary. An arbitrary density plot for the example: ...
user avatar
  • 264k
20 votes
Accepted

Element-wise test on List elements

To my knowledge, there aren't built-in versions for comparison operators that would be automatically threaded over lists. One reason for that is that Mathematica is a symbolic system, and every auto-...
user avatar
17 votes

Element-wise test on List elements

The BoolEval` package does exactly this. For example: BoolEval[{0.6, 1.2} > 1] (* Out: {0, 1} *) and ...
user avatar
  • 68.7k
16 votes
Accepted

Mapping on leaves

A little "secret" of level specifications is that they can be negative. -1 refers to the atomic leaves, -2 refers to all ...
user avatar
  • 224k
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 ...
user avatar
  • 132k
16 votes

Project map to a particular shape

If the domain $\varOmega$ of the county is simply connect, one might use the Riemannian mapping theorem. For $z_0 \in \varOmega^\circ$, we make the ansatz for the holomorphic map $f \colon \varOmega \...
user avatar
16 votes

Simplify the code

☺lookMaNoLetters☺ = 1 ## & @@@ # & /@ # &; ☺lookMaNoLetters☺ @ mylist {{y1 y2 y3, y3 y4 y5}, {w1 w2 w3, w4 w5 w6}} Further variations: ...
user avatar
  • 349k
16 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:...
user avatar
  • 7,666
15 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 ...
user avatar
  • 23.4k
14 votes

Apply function to every element of list regardless of depth

Use Map with a levelspec of {-1}: Map[g, {a, b, {c, d}, {{e}}}, {-1}] ...
user avatar
  • 2,788
14 votes
Accepted

Speed and the MapThread function

This is because of the compilation that kicks in automatically if the list in Map exceeds a certain number of elements. ...
user avatar
  • 68.7k
14 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/...
user avatar
  • 349k
13 votes
Accepted

Map a function of several arguments

There are many closely related topics but I've failed to find a duplicate. MapThread[Thread @* f, {First @ list1, list2}] MapThread[f, {list2, list3}] ...
user avatar
  • 132k
12 votes
Accepted

Operate over list of pure functions

This perhaps: Function[{a, b}, a[#]/b[#] &] @@@ {{a, b}, {c, d}, {e, f}} (* Out: {a[#1]/b[#1] &, c[#1]/d[#1] &, e[#1]/f[#1] &} *) Mr.Wizard's way ...
user avatar
  • 68.7k
12 votes

Speed and the MapThread function

Map is automatically compiled. Yes, even with RandomChoice. Try it: ...
user avatar
  • 22.5k
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 ...
user avatar
  • 224k
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

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: ...
user avatar
  • 3,807
11 votes
Accepted

Apply function to every element of list regardless of depth

molekyla777's answer can be very helpful but it is not technically correct. The question specifies "every element of a list" but using a levelspec of {-1} will ...
user avatar
  • 264k
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 ...
user avatar
  • 14k
11 votes

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

I changed D to Dd: MapAt[Dd, A, 2 ;; ;; 2]
user avatar
  • 3,807
11 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{...
user avatar
  • 15k
10 votes
Accepted

Is there a built-in function to map a list of heads to a list of data?

Update Well I guess I should retire for the evening to a less brain-intensive activity as apparently I can't think clearly. One could of course use Outer: ...
user avatar
  • 264k
10 votes

Can A Plot Be Overlayed With A Map?

I would prefer to use the "GeoImage" styling, because you can use other projections when using it. Let's say you have data for the whole world in a matrix: ...
user avatar
  • 3,932
10 votes

How to elegantly map multiple functions over a matrix?

This is really a natural fit for Outer: ...
user avatar
  • 95k
10 votes
Accepted

Bug when combining Dataset with a scoping construct and Map

$Version "11.0.0 for Microsoft Windows (64-bit) (July 28, 2016)" ...
user avatar
10 votes
Accepted

How to make a matrix of derivatives?

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

Combination of list elements

If you have to use Map and #1 + #2 + #3 & literally: ...
user avatar
  • 349k

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