Ali Hashmi
  • Member for 6 years, 10 months
  • Last seen this week
Create GIF looping animation
Accepted answer
27 votes

As partly mentioned in this: Add delay to the final frame of a GIF? we can use "AnimationRepetitions" -> ∞ to loop a GIF indefinitely: Export["C:\\Users\\Ali Hashmi\\Desktop\\test.gif", gif, "...

View answer
How to get cell boundaries in the image?
13 votes

I have somewhat mixed corey's and nikie's approach (check their posts) to arrive at a somewhat reasonable segmentation. Kudos to them. img2 = ImageAdjust@RidgeFilter[img, 5] // GaussianFilter[#, 8] &...

View answer
Reduce Code Length
Accepted answer
12 votes

since the result of the new computation depends on the previous one we can use NestList NestList[N@({0, 0, 0.5} + RotationMatrix[45 Degree, {0, 0, 1}].Transpose[#])\[Transpose] &, n01, 18]

View answer
Can logical operators be used with Cases?
12 votes

Cases[Range[-75, 100], _?(Positive[#] && PrimeQ[#] &)] (* or *) Cases[Range[-75, 100], _?(Apply[And, Composition[Positive[#], PrimeQ[#]]] &)] (* which is the same as the one below*) ...

View answer
How to merge data?
Accepted answer
11 votes

Using the straightforward way GroupBy Normal@GroupBy[{{2, 3} -> 1, {1, 5} -> 1, {1, 1} -> 2, {2, 2} -> 2}, Last -> First] (* {1 -> {{2, 3}, {1, 5}}, 2 -> {{1, 1}, {2, 2}}} *) ...

View answer
removing list containing zeros
Accepted answer
11 votes

list = {{12, 1, 23, 4}, {0, 0, 0, 0}, {34, 67, 5, 60}, {0, 0, 0, 0}}; DeleteCases[ list , {0 ..}] (* {{12, 1, 23, 4}, {34, 67, 5, 60}} *) (* this also results in the same answer *) Cases[ list , ...

View answer
How to find the position of elements in a list satisfying criteria
10 votes

For really big list or a rectangular matrix I would suggest the following approaches: list = RandomInteger[20, 1000000]; f1[list_,num_] := SparseArray[UnitStep[list-(num + 1)]]["NonzeroPositions"]; ...

View answer
How to implement lane-change rules for Nagel Schreckenberg model
8 votes

The code below has been fixed and is now working. It is based on RNSL model (a modified version of Nagel Schreckenberg model that works for the case of two lanes). Clear@func; With[{maxbound = 100, ...

View answer
Use a picture as the initial distribution of an agent based model
7 votes

you can use PixelValuePositions Show[image, MapThread[Graphics[{#2, Point@RandomSample[PixelValuePositions[image, #1], 2000]}] &, {{0, 1}, {Red, Blue}}]] of course there are some outlying ...

View answer
How can you Unset the following UpValues?
6 votes

a /: Except[TagUnset, _][___, a, ___] := (Print["a fired"]; Null); f[a] (* a fired *) (* As we can see below, usage of ClearAll does not work *) ClearAll[a] (* a fired *) (* now removing the ...

View answer
Test if number represents an integer
6 votes

on suggestion of @george2079 checkInteger[num_] := (# === 0. || # === 0) &@FractionalPart[num]

View answer
Calculating the mean of a list with NaN values
6 votes

Mean@Cases[{1, 2, 3, NaN, 2}, Except[NaN]] (* 2 *) Mean@DeleteCases[{1, 2, 3, NaN, 2}, NaN] (* 2 *)

View answer
Find Closest Path
6 votes

This gives all paths and not just the shortest path edges = {{1, 6}, {1, 7}, {2, 6}, {2, 7}, {2, 8}, {3, 7}, {3, 9}, {4, 8}, {4, 9}, {4, 10}, {5, 9}, {5, 10}, {6, 1}, {7, 1}, {6, 2}, {7, 2}, {8, 2},...

View answer
List all possible license plate numbers
6 votes

you can definitely use Outer if you prefer a = Alphabet[]; b = Range[0,9]; list = Flatten[Outer[List, a,a,b,b,b,b], 5]; list//Length (* 6760000 *)

View answer
The curious case of missing random-walking particles in the box
Accepted answer
6 votes

The problem has been finally resolved. I used some conditions to spit out the state of the system when any particle disappeared. I found that the main reason for particles to disappear was a pattern ...

View answer
Inserting some zeros in a list by a rule of positions
5 votes

mainlist={0.23, 0.34, 0.8, 0.0, -0.2, 0.4, -0.1}; positionlist={3,4,8,9,10,13,14}; ReplacePart[ConstantArray[0, 17], Thread[positionlist -> mainlist]] or what @Carl Woll suggested: finalresult = ...

View answer
Replace element in array by checking condition in another list
5 votes

list1 = {{1, 2, 0}, {1, 3, 0}, {4, 6, 0}, {2, 3, 0}, {3, 2, 0}}; list2 = {{3, 2, 1}, {1, 3, 1}, {4, 5, 1}}; replace[list1_, list2_] := Module[{temp, pos, val, l = list1}, temp = Outer[If[SameQ @@ ...

View answer
What is the idiomatic way to operate on the components of an image separately?
5 votes

Module[{seg, img = img, cm, newComps, func}, seg = MorphologicalComponents[img]; cm = ComponentMeasurements[{seg, ColorNegate@img}, {"MaskedImage","BoundingBox"}]; newComps = ImageAdjust /@ ...

View answer
Splitting nasty list FAILED
4 votes

uses ReplaceRepeated in a recursive manner to generate the lists StringReplace[ Cases[List1, x_ /; StringStartsQ[x, ("Id" | "CreationDate" | "Tags")]], ">" | "<" -> ""] //. {p___, ...

View answer
Replace Table by functional programming
4 votes

BlockMap[{{#1[[1]], #[[1]]^2}, {#1[[2]], #[[2]]^2}} &, Range[3, 6], 2] (* {{{3, 9}, {4, 16}}, {{5, 25}, {6, 36}}} *)

View answer
How to define an option or an argument whose values are confined
4 votes

ClearAll[testFunc]; Options[testFunc] = {opt -> 1}; testFunc[x_, OptionsPattern[]] := x^2 /; Range[4]~MemberQ~OptionValue[opt]; testFunc[x_, OptionsPattern[]] := "option provided out of range" ...

View answer
How to create an Identity matrix and replace the ''1''s with n which n is the elements' row or column number?
4 votes

here is another way using Unitize π^2 Table[i (1 - Unitize[Range[1000] - i]), {i, 1000}]; // AbsoluteTiming (* {0.552622, Null} *) and with SparseArray alone: π^2 SparseArray[{i_, i_} :> i , {...

View answer
A partial outer product
4 votes

S = {s1, s2, s3, s4}; ls = SparseArray[{i_, j_} :> Correlation[S[[i]], S[[j]]] /; i > j, ConstantArray[Length@S,2]] // Quiet; ls//Normal (* {{0, 0, 0, 0}, {Correlation[s2, s1], 0, 0, 0}, {...

View answer
extract replacement list with conditions
4 votes

Map[First@Cases[#, {x_ /; x == Min[First @@@ #[[All, 1]]], pat : __ } :> pat , Infinity] &, list] or in a more succint and better way proposed by Kuba: Last@*First@*MinimalBy[First]/@ list ...

View answer
Delete from list
4 votes

m = DeleteDuplicates[Map[Sort, Tuples[{a, b, c, d}, 3]]]; DeleteCases[m, {Alternatives@@Function[x, OrderlessPatternSequence[_, x, x], Listable], {b, c, d}]}] (*{{a, a, a}, {a, a, b}, {a, a, c}, {a, ...

View answer
Which condition?
Accepted answer
4 votes

basically if you deconstruct you will see that Element in your case operates on the sublist level {1, 2, 3} ∈ Integers (* True *) a ∈ Integers (* does not result in a boolean *) {4,5} ∈ Integers (* ...

View answer
Partition a list by count of a number
4 votes

here is a recursive way using tail-recursion and pattern matching Clear[func, partition,f]; func[x_List: {__}] := x /. {a___, patt : Repeated[PatternSequence[2, ___], {3}], c___} :> Join[{{a, ...

View answer
Create a particular {x,y} list
Accepted answer
4 votes

Tuples[Range[100], 2] or using Table as mentioned in the comment It is worth noting that the method relying on Tuples is 10-12 times faster than Table

View answer
How to implement Controls for operations on an image with this scheme?
Accepted answer
4 votes

I have been working on the problem for some days and came up with a naive/modest strategy to the answer. I am new to DynamicModule so it may not be the perfect solution, however the current code has ...

View answer
How to make AppendTo faster
4 votes

As J.M. has pointed out: using Table[] is a better idea, or else i modified your code a little bit with the use of Reap[] and Sow[] functions. It is on average 12 seconds faster: alphaxt = 100; ...

View answer
1
2 3 4 5 6