Ali Hashmi
  • Member for 6 years, 10 months
  • Last seen this week
Synchronize lists
2 votes

we can use FixedPoint with Replace Cases[FixedPoint[Replace[{p___, {x_, y_}, q___, {w_, z_}, r___} /; Abs[y - z] < 0.02 :> {p, {{x, y}, {w, z}}, q, r}], Join[list1, list2]], {{_, _}, {_, _}}]...

View answer
RegionIntersection Using an Arrow and a Circle
2 votes

a slight variant of @MarcoB 's answer (we can do without Unprotect): roi = {Circle[{0, 0}, 0.6`, {30 \[Degree], 80 \[Degree]}], Arrow[{{0, 0}, {1, 1}}]}; SetAttributes[regInt, HoldAllComplete]; ...

View answer
Select according to a function af a line of a table
2 votes

t = {{1, 2, 11}, {0, 2, 11}, {2, 1, 11}}; Extract[t, FirstPosition[Plus @@@ t, 14]] (* {1, 2, 11} *)

View answer
Is there a quick way to replace backwards after long sequence of ReplaceAll?
2 votes

rules = {a->b, c->d, e->f, g->h}; Solve[expr /. rules] /. (Map[Reverse]@rules)

View answer
Subtracting second columns of two matrices
2 votes

Thread[{list1[[All, 1]], list1[[All, 2]] - list2[[All, 2]]}] (* {{0.5, 0.2}, {0.75, -0.3}, {1.1, 2.1}, {1.3, 1.}, {2., 1.1}} *) Thread[{#1 & @@@ list1, (#2 & @@@ list1) - (#2 & @@@ list2)}...

View answer
Finding the number of $4$-tuples of consisting of digits $1$-$6$ satisfying a certain condition
2 votes

list = (Permutations[Flatten@ConstantArray[Range@6, 4], {4}]); Length@Select[list, And@@Thread[#[[1 ;; 3]] >= 5] && #[[4]] < 5 & ]/Length@list (* 2/81 *)

View answer
Find prefix of list in Association
2 votes

asc = <|{1} -> "a", {0, 1} -> "b", {0, 0} -> "c"|>; func[assoc_Association, code : {(1 | 0) ..}] := Map[assoc[#] &][SequenceCases[code, Alternatives @@ Keys@assoc]]; func[asc,{1, ...

View answer
Populate data with ratio to previous element
2 votes

Flatten[Riffle[#, Ratios@Prepend[#2 & @@@ #, Last@First[#]]], 1]&@data~Partition~3 (* {{1, 3, 1}, {2, 4, 4/3}, {3, 5, 5/4}, {4, 6, 6/5}} *)

View answer
Unitize matrix above certain threshold
2 votes

as suggested by @Szabolcs {{1.99719*10^7, 66451.9, 84170.3, 57308.1, 13392.5, 16138.1}, {130243., 1.21641*10^7, 61228.3, 66902.9, 53069.1, 30019.1}, {67442.3, 41337., 3.56545*10^6, 12429.4, 6761.89,...

View answer
Finding 25 points nearest to an approximation line
2 votes

pts = Transpose[{xx, yy}]; f = LinearModelFit[pts, {x}, x]; linepts = Thread[{#, Function[x, f[x], Listable][#]}] &@(Range @@ MinMax[xx]); distmat = DistanceMatrix[pts, linepts]; distance = ...

View answer
generate association based on list
2 votes

AssociationThread[# -> FromLetterNumber[#]] &@Range[4]

View answer
how do I multiply list elements by a constant if the element satisfies a condition
2 votes

Function[x, If[x > 0.5, 5 x, x], Listable][list] (* {-1, 0, 5, 10, 15} *)

View answer
Sign change of sine function
Accepted answer
2 votes

This implementation ensures that Sin would not evaluate immediately. ClearAll@times; SetAttributes[times, HoldAllComplete]; times /: times[Times[x_, Sin[y_]]] /; Positive[x] := x HoldForm[Sin[y]]; ...

View answer
Unexpected behavior of MapAt
2 votes

This may be the fastest way in my analysis so far and may compete with Fold or perhaps perform better Module[{m = Range@10}, SetAttributes[func, HoldFirst]; func[x_, {}] := x; func[x_, y_] := (x[[y[[...

View answer
Built-in singles-to-pairs ({a, b, c, d} -> {{a, b}, {b, c}, {c, d}})?
Accepted answer
2 votes

as mentioned by C.E. the preferred built-in function is: Partition[Range[5], 2, 1] (* {{1, 2}, {2, 3}, {3, 4}, {4, 5}} *) as pointed out by Simon Woods: SequenceCases[Range[5], {_, _}, Overlaps -&...

View answer
Replace heads to match a pattern
2 votes

Fold[Replace[#1, f[expr___] -> #2[[1]][expr], {#2[[2]]}] &, f[f[f[]]], {{a, 0}, {b, 1}, {c, 2}}] (* a[b[c[]]] *) however since we are replacing all the heads iteratively at each level, the ...

View answer
Traverse "curve" on a grid from a given cell
2 votes

With some credits to @C.E. (* which trajectory the point lies on *) whichTrajectory[grid_, startpos_] := With[{p = MorphologicalComponents[grid]}, With[{labels = (Union@Flatten@p)[[2 ;;]]}, {p, First ...

View answer
A function that accepts a list of pairs and adds new pairs that are the averages of the neighbouring pair values
2 votes

using Replace and recursion list1 = {{x1, y1}, {x2, y2}, {x3, y3}}; Clear@func; func[list_] := list /. {___, a : {_, _}, b : {_, _}, d : {_, _} ...} :> Join[{a, Mean[{a, b}]}, func[Join[{b}, {d}...

View answer
How can I subtract the components of these 2 solutions if the components are interpolating functions?
Accepted answer
2 votes

compacted notation by Bob Hanlon f[t_] := EuclideanDistance@@({x[t], y[t], z[t]} /.{sol1, sol2}) original attempt f[t_] := EuclideanDistance[({x[t], y[t], z[t]} /. sol1[[1]]), ({x[t], y[t], z[...

View answer
Sort Output List by Search Value
2 votes

I personally prefer @Kuba's method of KeySort. You can always use Normalthen SortBy and then Association ClearAll[dice, sides]; dice := 3 sides := 6 <| SortBy[First]@Normal@CountsBy[ Times @@@ ...

View answer
Find nearest distance of an interior point from perimeter
2 votes

RegionNearest can also be an option imp = Import["C:\\Users\\Ali Hashmi\\Downloads\\PI.mat","LabeledData"]; X = "KP" /. imp; Y = "KI" /. imp; threadeddata = Transpose[{Flatten@X, Flatten@Y}]; ...

View answer
SumConverge does not give a valid result
2 votes

previous post (may or may not be correct implementation) SumConvergence[(Sin[n])^2/n^p, p] (* this gives condition for convergence *) (* Abs[n] > 1 *) SumConvergence[(Sin[Abs@n > 1])^2/n^2, n] ...

View answer
Selecting list entries with a True False index list of similar length
2 votes

you can use Select without having to create the list of Boolean values True, Falseexplicitly Select[Range[10], Divisible[#, 3] &] (* {3,6,9} *) Alternatively one can use Extract Extract[#, ...

View answer
boolean casting
Accepted answer
2 votes

here is how you can implement it yourself: Clear@if; if[x_, y_, z_] := Module[{f, g}, Which[Head[x] === List, (f = Length@x > 0; Switch[f, True, y, _, z]), Head@x === Integer, (g = Mod[x, 2]; ...

View answer
Numerical approximation of $\pi$
2 votes

A slightly different approach: inside = Pick[pts, Map[# ∈ Disk[] &, pts], True]; outside = Complement[pts, inside]; Also as pointed in a comment above: inside = Pick[#, RegionMember[Disk[]][#], ...

View answer
How to make a better `Sort` in my case
2 votes

there is a nasty way of doing this: orig = (Riffle[ToCharacterCode[fileNames], (Characters /@ fileNames)]) // Partition[#, 2] & // Thread[#] & /@ # & // #[[All, {21, 22}]] & (*{{{54,...

View answer
Obtain multiple coordinates from a function at a particular value
2 votes

Perhaps not a good job but the closest i can get to: plot = Reap[ContourPlot[F[{x, y}], {x, -3, 3}, {y, -3, 3}, EvaluationMonitor :> If[5.95 < F[{x, y}] < 6, Sow[{x, y, F[{x, y}]}],]]][[2,1]]...

View answer
Replacement of a group of variables in trigonometric expression
Accepted answer
2 votes

try this: FullSimplify[ -Cos[γ[1][t] - θ[1][t] - ψ[1][t]] l[f] + m[f] Sin[γ[1][t]] + c[f] Sin[γ[1][t] - θ[1][t]] - h[f] Sin[γ[1][t] - θ[1][t] - ψ[1][t]] + x[P1][t] == (-Cos[γ[2][t] - θ[2][...

View answer
Corresponding list value and index in a list
2 votes

try this: First@Flatten@Position[#, Max[#]] &@list alternative: Last@Ordering[list]

View answer
Highlighting pattern strings in a large text
2 votes

SearchAll[sequence_, search_] := Module[{map}, map = Map[# -> "\!\(\*StyleBox[\"" <> # <> "\",FontColor->" <> ToString@RGBColor[RandomReal[], RandomReal[], RandomReal[]]...

View answer