Ali Hashmi
  • Member for 6 years, 10 months
  • Last seen this week
How to efficiently read blank line separated data file into list of lists
1 votes

you can do something like this: data=Import@"C:\\ Users \\ Ali Hashmi \\ Desktop \\ file.dat"; (* {{}, {1, 2}, {3, 4}, {5, 6}, {}, {11, 22}, {33, 44}, {55,66}, {}, {22, 33, 44}, {55, 66}, {}, {}, {2, ...

View answer
Linear Integer Programming for solving Sudoku
Accepted answer
1 votes

I found that the system has the tendency to generate results provided that the system is not too undetermined. for instance if we use the grid (same as above but with slightly fewer unknowns) and ...

View answer
Importing data surrounded by Square brackets
Accepted answer
1 votes

ToExpression[ StringReplace["[[1 4 5 6 2] [9 8 7 4 7]]", {" " -> ",", "[" -> "{", "]" -> "}"}]] (* {{1, 4, 5, 6, 2}, {9, 8, 7, 4, 7}} *)

View answer
Joining Array Values
Accepted answer
1 votes

@LouisB and I have propose the following solution Transpose /@ ArrayReshape[Transpose@{Aa, Bb, Cc}, {3, 3, 3}] or ArrayReshape[Transpose[Flatten /@ {Aa, Bb, Cc}], {3, 3, 3}] and as mentioned by @...

View answer
Data selection by comparing elements from different sublists in a nested list
1 votes

Here is a partial attempt to make your code faster. I would suggest the use of ParallelTable and SortBy rather than Sort. Perhaps in your case there is no need to use IntervalMemberQ and Interval. ...

View answer
Region and Infinite Line Intersection does not yield correct results with Maximize
1 votes

list = (RegionIntersection[\[ScriptCapitalR]2, #]&/@ MeshPrimitives[\[ScriptCapitalR]1, 1] //Cases[#, Point[arg_] :> arg] &); trimList[ls_] := Module[{p = ls, pairs, firstelem = First@ls, ...

View answer
Conditions on a sum
1 votes

Clear@fun; fun[n_, l_] := Plus @@ Function[x,Times @@ (n^x)] /@ (Select[Tuples[ Range[0, l], {Length@n}], Total@# == l && Length@# == Length@n &]) fun[{p, q}, 4] (* p^4 + p^3 q + p q^3 + ...

View answer
How to retrieve a sub-set of geometric primitives within arbitrary region?
1 votes

This is the most general solution i could conceive. added the SameQ test in Complement to get lines outside the region as well Module[{module, lines, points, area, region, memberpoints, ...

View answer
Creating an advance If statement nest with /; as conditions
1 votes

function8[data : {{_, _} ..}, min_,max_] := (Mean[data[[All, 2]]] /; AnyTrue[{Min[data], Max[data]}, IntervalMemberQ[Interval[{min, max}], #] &]) function8[data : {{_, _} ..}, min_, max_] := "No ...

View answer
Find all permutations with a condition
1 votes

(* a crude way: lets assume we wish to find all the permutations of a list of three integers that add up to 4 *) Select[Permutations[Flatten@ConstantArray[Range[0, 4], 3], {3}], Plus @@ # == 4 &]...

View answer
Average Clusters of x,y coordinates
1 votes

Map[(Total[#]/Length[#]) &, #] &@{{{10, 34}, {9, 32}, {9, 33}}, {{5, 18}, {3, 20}}, {{16, 21}}, {{20, 33}}, {{21, 18}}, {{6, 26}}, {{23, 22}}, {{15, 35}}, {{19, 26}}, {{24, 12}}, {{22, 25}}, {{...

View answer
How to get a deformable mesh from this binarized image?
1 votes

I thought about a scheme to perform two most important operations on the image mask i.e. delete edges (in over-segmented regions or incorrect edges) or add edges (in under-segmented regions) with a ...

View answer
How to renew a value when a value is larger than it, counting the time of the record being broken at the same time
1 votes

I am marking this question as a duplicate since you have asked a very similar question before. However, below is what you need to implement. The counter will count the number of times the value is ...

View answer
Texture a surface with a JPG image
1 votes

Based on the earlier suggestion: i = Import@"https://burnersxxx.files.wordpress.com/2013/08/einsteintongue.jpg" Plot3D[Sin[x + y^2], {x, -3, 3}, {y, -2, 2}, PlotStyle -> Texture[i], Mesh -> ...

View answer
Animated discrete plot for a generalised random walk
1 votes

I am not sure why you are assigning probabilities if according to you - and correct me if i am wrong - a point could just randomly be selected. I think you need a Metropolis Acceptance Criterion for ...

View answer
Indicate minimal path sum
0 votes

here i am modifying the answer given by @kglr slightly to find both the maximum and the minimum cost path. pathMatrix[matr_, Oper_: Max] := Module[{sum, nextF, i, j}, With[{dim = Dimensions@matr}, ...

View answer
How to get rid of Sequence in the arguments of a DownValue generated at runtime
Accepted answer
0 votes

ClearAll[addDownValuesRuntime, f] str = "p"; int = 5; (* global values defined to test if our code binds the definition to function f without evaluating these OwnValues *) SetAttributes[...

View answer
evaluate the bounds of iterator in the table given as a sequence?
0 votes

I constructed a method to overcome the issue: SetAttributes[table, HoldAllComplete]; table[body_, tail_] := Module[{p, q, sym = Unique["x"]}, {p, q} = Map[(Hold[#] /. x_ /;(Developer`HoldSymbolQ[x] ...

View answer
Extracting the integers from a list
0 votes

if order does not matter: Attributes[f] = {Orderless}; f[x__Integer, __] := {x} f @@ {3, 5.6, 8.19, 2, 5.6, 4, 3, 8.5, 4.137, 7., 1.165} (* {2, 3, 3, 4} *)

View answer
Need a function that, given f, a function of two arguments, calls f on successive elements of the list and the result of the last evaluation of f
0 votes

Map[ Reverse@Array[a, #] //. {a[x_], p : Repeated[a[_], {2, Infinity}]} :> {a[x], {p}} /. List :> f &, Range[5]] (* {a[1], f[a[2], a[1]], f[a[3], f[a[2], a[1]]], f[a[4], f[a[3], f[a[2], a[...

View answer
Assign an array of numbers to an array of variables
0 votes

you can also do this: Inner[Set, Most@variables, Most@values, None];

View answer
Measuring what fraction of an image is covered by filaments
0 votes

A HeadStart by no means it is a complete answer. But one can surely play around with it a bit img2 = MorphologicalBinarize[GaussianFilter[BrightnessEqualize[img], 2], 0.44] // Opening[#, 2] & // ...

View answer
Referring to arguments passed to function
0 votes

you can also do something like this: f[arg__][p_] := p @@ {arg} Through[{f[1, 2], f[Sin[x], Cos[x], 4, 7]}[List]] (* {{1, 2}, {Sin[x], Cos[x], 4, 7}} *)

View answer
Find Array Elements That Meet a Condition
0 votes

Loops are not usually needed in Mathematica to solve problems. Functional Programming and Patterns can do job more efficiently m = {-1, -3, -2, -5, -4}; {Flatten@#, Extract[m, #]}\[Transpose]&@...

View answer
DeleteCases based on string length
0 votes

Map[Cases[__?(StringLength[#] >= 2 &)]][data] Map[Cases[x__/;StringLength[x] >= 2]][data]

View answer
How to find most complete partition of a list
0 votes

ClearAll@completePartitions; completePartitions[list1_, list2_] := Module[{permute, possibleCombinations, flattenedComb, func, listinplace = list1, pick}, permute = Subsets[list1, All]; ...

View answer
Exclude zero when sampling random integers
0 votes

This will delete all entries with zeros: t = Table[a -> RandomInteger[{-9, 9}, 3], {50}]; DeleteCases[t, HoldPattern[a -> p : {__}] /; MemberQ[p, 0]] also this will replace entries where zeros ...

View answer
How to construct pairs in a list?
0 votes

constructPairs[input_] := Block[{func, f, list1, permutepart, list2}, func[x[t_]] := t; func[x[t_]^m_] := ConstantArray[t, m]; list1 = Flatten[Map[func]@Cases[input, p : (t_x | x[_]^_) :> p]]; ...

View answer
Extract the first element in the list when second element is minimal
0 votes

SeedRandom[2]; v = RandomInteger[99, {7, 2}]; First@SortBy[v, Last] (* {63, 1} *) also Extract[#, Position[#[[All, 2]], Min[#[[All, 2]]]]] &@v

View answer
Easiest way to extract the coefficient of a polynomial
0 votes

for your particular example, this is quite simple: FirstCase[387 a1^4 a2^3 x^3 y^7 z^100 w^364, _Integer] (* 387 *) this case works even with integer being not in the first position: FirstCase[a1^4 ...

View answer