# Tag Info

1

Since I don't have SequencePosition: ReplaceSequence[list_, fr_, to_] := With[{le = Length@fr}, ReplacePart[ list, Dispatch[ Rule @@@ Level[ Map[Transpose[{#, to}] &, Map[Range[#, # + le - 1] &, Flatten@Position[Partition[list, le, 1], f]]], {2}] ]]] ReplaceSequence[RandomInteger[1, 50000], ...

3

Using sequenceReplace defined here, you can do It as: list = {{0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0}, {0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0}} sequenceReplace[#, {0,0,1,0,0}:> Sequence@@{.5, .5, 1, .5, .5}]&/@list

4

There is the internal GeneralUtilitiesStringPatternQ: ? GeneralUtilitiesStringPatternQ StringPatternQ[expr] gives True if expr is a valid string pattern, suitable for use with e.g. StringMatchQ It is a predicate that allows for the most inclusive match, thus it returns True for any pattern that could stand for a string pattern. ...

1

Ad 1 After commenting the second definition, fun[x,y] was evaluated and left in this form since no definition was provided for symbolic arguments. Then the replacement was done and tutorial/Evaluation says: [...] in evaluating an expression like h[e1, e2, ...]. Every time the expression changes, the Wolfram Language effectively starts the evaluation ...

2

Note what happens when you evaluate Abs[u] > 0.01, for a u with no value of its own: In[1]:= ClearAll[u]; Abs[u] > 0.01 Out[1]= Abs[u] > 0.01 Nothing! Mathematica can't figure out a value for Abs[u] without knowing anything about u, so it leaves the expression unchanged. This means that a conditioned pattern like u_ /; Abs[u] > 0.01 isn't ...

8

How about Position[c, I, {1}] or Position[N@c, N@I] Then I'd like to point out that, your explanation "what Mathematica does here is to apply the Position function on FullForm[c]" is incorrect, because FullForm always represents the essence of the code, Position is just trying to find the FullForm of the pattern in the FullForm of the list, it has ...

1

Just for some ridiculousness (covering some of the give answers): t = {a, b, c, d, e}; ctl = {2.3, 0, 5, 0, 0}; Various: Pick[t, Positive@ctl] (* JM*) Extract[t, Position[ctl, _?Positive]] (* bill s *) Cases[Transpose[{t, ctl}], {x_, _?Positive} :> x] First /@ Select[Transpose[{t, ctl}], #[[2]] > 0 &] Last@Reap[Sow @@@ (Transpose@{t, ctl}), ...

2

Update Based on OP's comments, here's how I would go about solving this problem pre-V10.1 (the OP is correct that SequenceCases is the best option here, but it was introduced in V10.1). We want to find all runs, where a "run" is defined as a sequence of elements in which the last element in the list is always positive, and the run ends when the last element ...

4

You use a PositionIndex with KeySelect for this, which is a good approach if you need to use multiple tests against the same array. In[1]:= T = {a, b, c, d, e}; Ctl = {2.3, 0, 5, 0, 0}; In[2]:= index = PositionIndex[Ctl] Out[2]= <|2.3 -> {1}, 0 -> {2, 4, 5}, 5 -> {3}|> In[3]:= Extract[T, Values@KeySelect[index, Positive]] Out[3]= ...

10

You can find the position/index within an array of all values in a specified interval, (like Matlab's find) using Position. For instance: pos = Position[a, _?((0.3 < # < 0.7) &)] or pos = Position[a, x_ /; (0.3 < x < 0.7)] These find the indices of all elements in a with values between 0.3 and 0.7. The elements can be extracted from a ...

8

There is another way that is on my machine almost 500x faster then your solution. The idea is to look how Mathematica represents colored strings and use this directly. When we colorize an input string by selecting text and using the Format menu, we can create something like this Now, press Ctrl+Shift+E to see the underlying expression. ...

4

I really enjoy Mathematica when I can outsource tough algorithmic decisions to their source code- I believe this is the case here. It appears as if your code is doing something expensive (searching and replacing) many different times. I propose to do it all at once. Benchmark: txt = ExampleData[{"Text", "AeneidEnglish"}]; somewords = ...

Top 50 recent answers are included