Tag Info

Hot answers tagged

12

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, ...


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]; ...


9

If I understand the statement, you wish to do this: Table[If[time[[i]] == 0, x[[i]] y[[i]], z[[i]]], {i, 1, Length[time]}] but you wish to do it without Table or Do. One way to accomplish this is quite straightforward: (1 - time) x y + time z which gives the same result as above, though this assumes that the time variable is either zero (when you wish ...


8

First of all, let's clarify that if you define h as `h[{x_, y_}] := ...` then it takes a single argument which is a list of two items. If you define it as `h[x_, y_] := ...` then it takes two separate arguments. #n denotes the nth argument in a pure function. In the function call (#1^#2)& [{2,3}] you are passing the pure function a single ...


8

This is a good example where Internal`PartitionRagged (IPR) can be used very effectively. First, observe the following: list = {10, 20, 30, 40, 50, 60, 70}; (* 3 continuous elements in the middle *) Internal`PartitionRagged[list, {2, 3, 2}] (* {{10, 20}, {30, 40, 50}, {60, 70}} *) (* 3 continuous elements from the start *) Internal`PartitionRagged[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

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]


7

Another way, using MapIndexed's functionality, like rm-rf's: mapAtIndexed[f_, expr_, pos_, levelspec_: 1, opts : OptionsPattern[MapIndexed]] := Module[{f0}, f0[x_, p : Alternatives @@ pos] := f[x, p]; f0[x_, _] := x; MapIndexed[f0, expr, levelspec, opts] ] OP's example: mapAtIndexed[g, list, {{2}, {3}}] (* {10, g[20, {2}], g[30, {3}], 40} *) ...


7

I like the following very much {x, y} = {{1, 2, 4}, {3, 4, 6}}; (#1^2 + #2^2) & @@@ Tranpose[{x,y}] Another thing which is highly unused is to attach Attributes to pure Functions Function[{a, b}, a^2 + b^2, {Listable}][x, y] You can stick with the Slot notation too, but you have to tell then that the variables in Function is a Null list which might ...


6

Generic element-grouping function Several years ago, I wrote a simplistic HTML parser, for which I wrote a generalization of the function you request, that works on different levels of expression, and groups elements at specified start and end positions in specified heads. Implementation Here is the code (I made no effort to improve it, so it may not be ...


6

For educational purposes, here's a couple other ways to do this: Power @@@ {{1, 2}, {2, 2}, {3, 2}} Power[Sequence @@ #] & /@ {{1, 2}, {2, 2}, {3, 2}} Cases[{{1, 2}, {2, 2}, {3, 2}}, List[x__] :> Power[x]] # /. List -> Power & /@ {{1, 2}, {2, 2}, {3, 2}} Replace[{{1, 2}, {2, 2}, {3, 2}}, List -> Power, {2}, Heads -> True] ...


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 ...


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}


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

But it doesn't work. Why is that? It becomes visible when you inspect the inner Map only. I replace the slot for the outer function with 1, because we don't need it to see the error b = {1, 2}; c = {1, 2, 3}; Map[f[# &, 1], b] (* {f[#1 &, 1][1], f[#1 &, 1][2]} *) This is not what you expect and when you look a bit closer, you instantly ...


5

spanMap[f_, list_, s_, k_] := Join[list[[;; s - 1]], {f@list[[s ;; k]]}, list[[-Length@list + k ;;]]] list = {10, 20, 30, 40, 50, 60, 70}; spanMap[f, list, 1, 4] spanMap[f, list, 1, 7] {f[{10, 20, 30, 40}], 50, 60, 70} {f[{10, 20, 30, 40, 50, 60, 70}]} If You want safer version ...


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

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@#}]] ...


4

How about this way: spanMap[f_, list_, {start_, end_}] := Replace[ MapAt[f, list, List /@ Range[start, end]] /. {x___Integer, seq : (PatternSequence[f[_] ..]), y___Integer} :> {x, {seq}, y} /. f[n_] :> n, lst_List :> f@lst, 2] UPDATE: As there are three solutions I did some benchmarking to see which one is faster. I used the ...


4

bill already showed how to do this very efficiently with a numeric approach, but a bit closer to what you attempted and your direct request is MapThread: SeedRandom[1] time = RandomInteger[1, 20] MapThread[If[# == 0, #2*#3, #4] &, {time, x, y, z}] {0, 0, 0.805343, 0, 0.805343, 0.805343, 0.805343, 0, 0.805343, 1, 0.805343, 0.805343, 0.805343, ...


4

As expressed in the comments, the Replace functions are not merely "syntactic sugar" for Map. The two are quite different. One primary difference is the order in which expressions are visited. See: How to perform a depth-first preorder traversal of an expression? Another is that Replace will go inside held expressions, while Map does not evaluate: Hold[1 ...


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

Level one version This is an adaptation of amr's answer (based on Kuba's answer) mapAtLevOneIndexed[f_, list_, pos_] := ReplacePart[list, Inner[Rule[#, f[#2, #]] &, pos, Part[list, pos], List]] Example mapAtLevOneIndexed[f, {1, 2, 6, 7}, {2, 3}] -> {1, f[2, 2], f[6, 3], 7} In the case you work at level one, I think the most convenient ...


4

Here's a form similar to Kuba's approach: mapAtIndexed[f_, list_, pos_] := ReplacePart[list, # :> f[list[[Sequence @@ #]], #] & /@ pos]; A pure pattern version: mapAtIndexed[f_, list_, pos_] := ReplacePart[list, i : (Alternatives @@ pos) :> f[list[[Sequence @@ i]], i]]; And I assume you're familiar with Position.


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

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


3

TakeWhile[Reverse@Map[StringJoin, SortBy[GatherBy[DeleteDuplicates[ ToLowerCase[Characters[DictionaryLookup[x_ ~~ y_ ~~ z_ ~~ w_]]]], Union], Length], {2}], Length@# > 5 &] (* {{"ales", "elsa", "lase", "leas", "lesa", "sale", "seal"}, {"ates", "east", "eats", "etas", "sate", "seat", "teas"}, {"alts", "last", ...


3

You say that your data has the form of complex values, so I will make up some complex-valued data and a range of time: data = RandomReal[{-1, 1}, {10, 10}] + I RandomReal[{-1, 1}, {10, 10}]; time = Range[0, 1, 0.05]; Now consider a function f[x_, t_] := Abs[x]^t The goal is to "apply the function to every element of the data, for every timepoint." One ...


3

I'm sure one can improve following solution SetAttributes[mapIndexedAt, HoldRest]; mapIndexedAt[f_, list_, pos_] := Do[list = MapAt[f[#, pos[[i]]] &, list, pos[[i]]] , {i, Length@pos}] l = {1, 1, 1, 1}; mapIndexedAt[(#1 + #2) &, l, {2, 3}] {1, 3, 4, 1} It does not look good but at least it is not scanning ...



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