Tag Info

Hot answers tagged

14

These three functions are similar (speaking commonly), and in some applications any of them could be used, yet they have very different special applications. Rudimentarily: Map wraps (sub)expressions in a given Head, and returns the modified input Apply replaces Heads in (sub)expressions, and returns the modified input Scan "visits" (sub)expressions, ...


14

Preamble This is a very good question, because answering it will make it very clear what immutability means, both in general and in the context of Associations. General A few general words on immutability Associations are immutable data structures. This means that they carry no state, and a copy of an Association is another completely independent ...


11

When experimenting with Map (do check the examples under this link), it's better not to define the function you're mapping. If it's not defined, it won't evaluate and it's easier to see what's going on. Map[f, 1+x] is Map[f, Plus[1,x]] with a different notation. So you get Plus[f[1], f[x]], i.e. f[1]+f[2]. Map[f, x] returns x because x is an atomic ...


10

I believe I have a solution for you, assuming we've worked out all the discrepancies in the original question. You will need my dynamicPartition function or one of its "core function" equivalents. process[data_List] := Module[{f, s1, s2}, f[_, {1, ___, 1}] = 1; f[_, {___, 0}] = 0; f[x_, _] := x; s1 = Split @ Rest @ FoldList[f, 0, data]; ...


8

MapThread[Thread[{##}] &, {lst2, lst1}] Map[Thread, {lst2, lst1}\[Transpose]] ## is used so Thread gets called like Thread[{1, {a, b, c}}] As MapThread gives two arguments in this case it is equivalent to Thread[{#1, #2}]& and Composition[Thread, List]


8

This is an interesting puzzle. I think, though, you should restrict yourself to only words of length 4, to save processing time, as follows: words = DictionaryLookup[Repeated[CharacterRange["a", "z"], {4}]]; where I used CharacterRange to eliminate proper names and contractions. This has the side effect that all accent marks, umlauts, etc. are also ...


8

I hesitate to add anything after @Leonid's comprehensive answer, but I'd like to point out that an easy way to achieve the stated goal is to define f like this: f[x_] := <| x, "isFirstValueTrue" -> x@"firstValue" |> ... which yields the desired result when mapped across the associations in x: f /@ x (* { <|"firstValue" -> True, ...


7

If you look at SystemOptions[], like so, Column[ OpenerView /@ (Replace[SystemOptions[], Rule[x_, y_] -> List[x, y], 1]) ] you see that under CompileOptions, if you click on the triangle to open it, there is an option "MapCompileLength" -> 100. Set it to eg 10 and see it it helps (do SetSystemOptions["CompileOptions" -> ...


7

Use Map with a levelspec of {-1}: Map[g, {a, b, {c, d}, {{e}}}, {-1}] {g[a],g[b],{g[c],g[d]},{{g[e]}}}


6

Very similar to ssch's second answer, but sometimes Thread feels more natural than Transpose: Thread /@ Thread @ {lst2, lst1} Less clear, but more interesting, is to make a Listable version of List: Function[, {##}, Listable][lst2, lst1] You could also use my smartThread function: smartThread @ {lst2, lst1}


6

A Map/MapThread-less solution Transpose@Inner[List, lst2, lst1, List]


6

Consider this: fn[img_Image][t_] := ImageApply[Evaluate[# (1 + Sin[2 Pi*t])] &, img] times = Table[i, {i, 0, 1, 0.1}]; fn[lena] /@ times I used SubValues notation for flexibility (direct mapping onto times) but it is not required. You can also use the function like this: Table[fn[lena][i], {i, 0, 1, 0.1}] I pre-evaluated the body of the ...


5

A fast way to do it is to transpose MB to swap levels 3 and 4. Plus will then automatically thread over the arrays, after which you transpose back again: Mout = Transpose[MA + Transpose[MB, {1, 2, 4, 3}], {1, 2, 4, 3}]; In the question Kuba linked to, my smartThread function attempts to automate this method. You would use it like this: Mout = ...


5

Edit You had an error in the transcription of the equation for y[t]: a = 8.0; d = 0.2; g = 0.4; m = 15; s = 0.4; data = RecurrenceTable[{x[0] == 17, y[0] == 30, x[t] == (1. - a s) x[t - 1] + a (s m + (g*(s m/d - y[t - 1])) + ArcTan[x[t - 1] - m]), y[t] == (1. - d) y[t - 1] + s m + g*(s m/d - y[t - 1]) + ArcTan[x[t - 1] - m]}, {x, y}, {t, 0, ...


5

Here are two ways to do it. data = {{1, 3, 5}, {4, 5, 1}, {4, 9, 2}}; Replace[data, {a_, b_, c_} :> {a, b, f[b, c]}, {1}] {{1, 3, f[3, 5]}, {4, 5, f[5, 1]}, {4, 9, f[9, 2]}} MapThread[{#1, #2, f[#2, #3]} &, data] {{1, 3, f[3, 5]}, {4, 5, f[5, 1]}, {4, 9, f[9, 2]}} A small variant of the last is MapThread[{#1, #2, f[##2]} &, data]


5

When the sublists are short like in your example, I tend to use Apply: {#1, #2, f[#2, #3]}& @@@ {{1, 3, 5}, {4, 5, 1}, {4, 9, 2}}


5

Here's a way to write out the map concisely: Q1: {1 - #, ##2} Through[Join[{1 &}, f]@#] & @@@ A (* {{1 - a1, b1 f1[a1], c1 f2[a1], d1 f3[a1], e1 f4[a1]}, {1 - a2, b2 f1[a2], c2 f2[a2], d2 f3[a2], e2 f4[a2]}, {1 - a3, b3 f1[a3], c3 f2[a3], d3 f3[a3], e3 f4[a3]}} *) Q2: Join[{1 - #[[1]]}, MapThread[#3 #@#2 &, {f, Most@#, Rest@#}]] ...


5

In your first example, Map maps the function f over Plus, applying f to each argument. In the second case this is not possible, because there is just an x. What is your expected result? Maybe this? Map[f, {x}] (* {2 x} *)


5

fileNames = "file" <> # & /@ RandomSample[CharacterRange["A", "Z"], 4] (* {"fileS","fileU","fileJ","fileO"} *) sheetNames = "sheet" <> # & /@ # & /@ (RandomSample[CharacterRange["A", "Z"], RandomInteger[{1, 3}]] & /@ Range[4]) (* ...


4

Shorter: Position[myList, {_, #}] & /@ charList Since I feel that the answer is a little short, here's another way: charList /. MapIndexed[Last@# -> First@#2 &, myList]


4

Here is my refactoring of your code: inputList = RandomReal[100, {1000, 60, 2}]; selectedIndices = RandomInteger[{1, 30}, {10^3, 2}]; scalingFactor = 10; f1[m_] := With[{t = m\[Transpose]}, {#, Subtract[t, #]\[Transpose]} & /@ m] {listX, listY} = Round[scalingFactor * f1 /@ inputList[[{1, 2}]]]; pairList = {listX[[#, 1]], listY[[#2, ...


4

You can also do it using GatherBy: GatherBy[DictionaryLookup[Repeated[_, {4}]], Composition[Sort, Characters, ToLowerCase]] /. {_} -> Sequence[] (* {{"abbe", "babe"}, {"Abby", "baby"}, {"abed", "bade", "bead"}, {"Abel", "able", "bale", "Bela", "Elba"}, {"abet", "bate", "beat", "beta"}, << big list >> *) Of course, this also ...


4

Padding additional brackets is not the right way to do it. You should use the right function for the task, which is Map: matrices = {{{3, 2}, {2, 3}}, {{3, 2}, {2, 3}}}; Eigenvalues /@ matrices If you're insistent on using Apply (why?), then the following ways work: Eigenvalues[{##}] & @@@ matrices Eigenvalues @@@ List /@ matrices


4

This already seems to be answered in the comments, but since some 40+ minutes have passed I'll post it myself. Using george's example and Trace it's pretty clear what happens: MapIndexed[#2 &, {a, {b, c}}, Infinity] Is transformed into: {(#2&)[a,{1}],(#2&)[{(#2&)[b,{2,1}],(#2&)[c,{2,2}]},{2}]} From there it evaluates as usual and ...


4

If you consider the following lists: SeedRandom@1; list1 = RandomReal[{0, 10}, {10, 2}]; SeedRandom@2; list2 = RandomReal[{0, 10}, {10, 2}]; One can easily Map ListPlot by doing: Map[ListPlot, {list1, list2}] (* eq. to: ListPlot /@ {list1, list2} *) If you want to use Options with the ListPlot you need to define a function with the options in place. ...


4

MapThread[ expr /. {u -> #1, x -> #2[[1]], y -> #2[[2]]} &, {uRep, xyRep}] works as expected. So do (expr /. {u -> #1, x -> #2[[1]], y -> #2[[2]]}) & @@@ Transpose[{uRep, xyRep}] and (expr /. Thread[{u, x, y} -> ##]) & /@ (Flatten /@ Transpose[{uRep, xyRep}]) All three give {u1 == a1 + a2 x1 + a3 y1, u2 == a1 + a2 ...


4

Why don't you use? f@x and f@(1 + x) Also possible: Map[f, x, {0}] Further: Map[f, 1 + x, {0}] --> 2 (1 + x) Map[f, 1 + x, {1}] --> 2 + 2 x


3

MapThread[With[{n = #2}, {n, #} & /@ #1] &, {lst1, lst2}] {{{ 1, a}, {1, b}, {1, c}}, {{2, d}, {2, e}, {2, f}}}


3

An Outer version: Flatten[ MapThread[ Outer[List, {#1}, #2] &, {lst2, lst1}], 1] {{{1, a}, {1, b}, {1, c}}, {{2, d}, {2, e}, {2, f}}}


3

Your assertion that Map does not accept a levelspec of {0} is incorrect: Map[f, bar[1, 2, 3], {0}] f[bar[1, 2, 3]] For your second definition: Percent2[v_] := Map[Differences@#/Most@# &, v, {Length@Dimensions@v-1}]*100 Which duplication are you referring to, and why don't you like Slot notation? Anyway, if you are merely looking for a ...



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