Ali Hashmi
  • Member for 6 years, 10 months
  • Last seen this week
why this simple code is not giving the right answer under Compile
3 votes

the problem was in the comparator operator i.e. the IF statement. I have used the difference and Chop as the fix: If[Chop[ptTri[[1]] - source, 10^-8] == {0., 0., 0.}...] rather than If[ptTri[[1]] == ...

View answer
RegionMember does not work with RandomPolyhedron or Polyhedron object
Accepted answer
3 votes

here is a little fix that should work for both cases: datamodified = data /. {0.4500000000, 9.803074361, -0.2935999100} -> {0.4500000000, 9.803074361, -0.3935999100} (* this will make vertices of ...

View answer
Graphics`Mesh`FindIntersections[ ] fails to detect intersections
3 votes

I think the problem might be in the way we preprocess the image. Here is a fix: (* finding the boundary *) i = Import@"http://i.stack.imgur.com/PcWcz.png"; img = MorphologicalPerimeter@Binarize@...

View answer
What's the foo such that foo[{a, b, c}, 2] produces {a, a, b, b, c, c}?
3 votes

ff[v_List, n_Integer] := Last@Reap[Do[Sow[ConstantArray[i, n]], {i, v}]]~Flatten~2; ff[{3, {1, 4}, 1}, 3] (* {3, 3, 3, {1, 4}, {1, 4}, {1, 4}, 1, 1, 1} *) another way: foo[{}, _] := {}; foo[list_, ...

View answer
Replacement inside held expression
3 votes

Based on solutions proposed by @Leonid and @WReach I have noticed something peculiar with the use of Block and With for the Trott-Strzebonski solution. Consider the following code: f[x_] := x^2; g[...

View answer
How to put some number of queens on chess board dynamically?
3 votes

Adapted from C.E. here is a way to do it with recursive backtracking. We backtrack in this solution because I use a variable which is accessible to all the local scoping constructs and is deliberately ...

View answer
using SequenceHold as an argument to a Function
3 votes

We can conveniently do it using SequenceHold as the function attribute and passing the values with a head Sequence to the Function Function[{x}, p@x, SequenceHold][Sequence[1, 2, 3]] (* p[1, 2, 3] *) ...

View answer
Understanding PatternTest inside Position
3 votes

points = {{0, 1, 2}, {2, 4, 2}, {2, 1, 2}, {2, 3, 4}}; check[list_] := DeleteDuplicates[Delete[list, 2]] == {2}; Position[points,Except[List, _?check], {1}] (* {{2}, {3}} *)

View answer
How to select some sublist in a list of list
Accepted answer
3 votes

Map[Total, b, {2}] (* {{2, -2, 0}, {-2, 2, 0}, {-1, -1, 2}} *) c = Extract[a, Position[Map[Total, b, {2}], 2]] (* {{0., 0.5, 0.4}, {0.5, 0., 0.4}, {0.6, 0.6, 0.}} *) DeleteCases[c, 0., {2}] (* {{0.5,...

View answer
Any faster approach than Outer for my computation
3 votes

so far i have found that Compile does a pretty neat job in speeding up the computation func = Compile[{{list1, _Integer, 1}, {list2, _Integer, 1}}, Outer[Abs[#1 - #2]/Max[#1, #2] &, list1, list2]...

View answer
Rotating squares
3 votes

rr = {{t, -1}, {1, t}, {t, 1}, {-1, t}}; rotations = Map[RotationTransform[#][rr] &, Range[0, 2 Pi, 2 Pi/5]]; ParametricPlot[rotations, {t, -1, 1}]

View answer
Adding Rule Solutions
3 votes

alternatively we can use GroupBy list = {{x -> 1, y -> 2}, {x -> 2, a -> 5}}; Normal@GroupBy[Flatten@list, Keys -> Values, Total]; (* {x -> 3, y -> 2, a -> 5} *)

View answer
How to replace derivation sign(example: u^(1,2,0)) with it's some permutation?
3 votes

expr /. {(p : Derivative[PatternSequence[1, 0, 0]])[u] :> Operate[RotateRight, p[u]], (q : Derivative[PatternSequence[0, 1, 0]])[u] :> Operate[RotateLeft, q[u]]}

View answer
Drop From One list and Take in other list
3 votes

This approach uses SequenceCases SequenceCases[list, {{_?(# <= 3 &)} ..} | {{_?(# > 3 &)} ..}] (* {{{1}, {2}, {3}}, {{4}, {5}}} *)

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

{comps, boxes} = Module[{seg, img = img, cm, newComps, bounds}, seg = MorphologicalComponents[img]; cm = ComponentMeasurements[{seg, ColorNegate@img}, {"MaskedImage","BoundingBox"}]; bounds = cm[[All, ...

View answer
How to efficiently read blank line separated data file into list of lists
3 votes

ReadList["C:\\Users\\Ali Hashmi\\Desktop\\file.dat", String, NullRecords -> True] // SequenceCases[#, {Except[""] ..}] & // ToExpression@*StringSplit /@ # & (* {{{1, 2}, {3, 4}, {5, 6}}, ...

View answer
Number walls with mathematica
3 votes

Function to solve a wall with unknown parameters solveWall[list_List] := With[{sym = Cases[list, _Symbol, {2}]}, FindInstance[#, sym, Integers] &@(And @@ Map[x \[Function] First@x == Last@x, #...

View answer
How to define Normal for new "data types"
3 votes

using TagSetDelayed as pointed by @J.M. and @Leonid FractionSum /: Normal[FractionSum[{patt : RuleDelayed[_, _Symbol] ..}]] := With[{sym = {patt}[[All, 2]]}, Plus @@ (Power[#, -1] &@Table[First@...

View answer
Converting CSV data to a Dataset
3 votes

data = {{"No", "Vol", "Vel"}, {1, 500, 45}, {2, 700, 67}, {3, 350, 87}, {4, 123, 23}, {5, 587, 45}, {6, 435, 89}, {7, 896, 65}, {8, 125, 45}, {9, 476, 27}, {10, 987, 80}} dataset = Dataset[...

View answer
Suppress computation in table
3 votes

ClearAll[f]; f[x_List] := Table[Hold[{#}] &@(x[[i]] + x[[i + 1]]), {i, 1, Length[x] - 1}] Defer@@(f[{x, y}] + f[{x, 0}] + f[{y, 0}]) /. Hold[arg_] :> arg (* {x} + {y} + {x + y} *)

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

Module[{module, lines, points, area, region, pointsinout, linesinout, memberpoints, lineswithin, indexshared, func, posTrue, thread, linesoutside}, module = {{0, 0}, {0, 43}, {11, 43}, {11, 45}, {15,...

View answer
how to increase the y axis of a plot after getting plot?
3 votes

you can change the value of y_min and y_max to change the range on y axis (see below). Btw, instead of posting an open ended or a vague question, try to post your code to demonstrate your effort Plot[...

View answer
How can I extract the x and y values from a coordinate pair?
3 votes

there is another way as well: #1 & @@ {1, 2} (* gives 1 *) #2 & @@ {1, 2} (* gives 2 *) for multiple coordinates in a list: data = RandomInteger[{1, 100}, {10, 2}] #1 & @@@ data (* ...

View answer
Measuring distances between image components
2 votes

not a general answer (it is specific to the maze above or when the pixel distance is very small). Assuming distance to be Euclidean, the code below finds distance between neighbours (ignores any pair ...

View answer
Help to create an array
2 votes

With[{n = Range @ 256}, Reverse @ Table[UnitStep[n - i], {i, 256}]]; // RepeatedTiming (* {0.000244, Null} *) Relatively small example:

View answer
using Image Correlation for Particle Image Velocimetry
2 votes

Full Code on github: https://github.com/alihashmiii/simple-piv/blob/master/flowtrack.m PIV[image1_?ImageQ,image2_?ImageQ,win_Integer,pivmethod_]:=Module[{windowsize=win, imgDim=ImageDimensions[image1]...

View answer
Setting numerator terms to zero only
2 votes

expr = {(256 m2)/(ϵ M (-1 + b x) (1 + b x)), (256 ϵ m1)/(M^3 (-1 + b x) (1 + b x))}; Replace[#,Times[PatternSequence[___, Verbatim[ϵ]]] -> 0, {1}] & /@ expr (* {(256 m2)/(M (-1 + b x) (1 + ...

View answer
How to use Cases for nested list?
2 votes

You have answered your own question where you posted the question. Alternatively, Cases[list2, x_ /; Plus @@ x < 0.5]

View answer
Edit sublists of a list according to the length of the sublist
2 votes

quite similar to @eldo , you can use Replace list = {{a, b, c, d}, {e, f}, 32, {}, {g, h, i, j}, {k, l}, 32, {}, {m, n, o, p}, {q, r}}; Replace[list, {{x1_, x2_, x3_, _} :> {x1, x2, x3, z}, {_, ...

View answer
Mean of a list of lists (vector) with missing values
2 votes

2nd part of your question: list1 = {Missing[], 2, 3, Missing[], 6}; list2 = {4, Missing[], 1, Missing[], 1}; MapThread[If[FreeQ[{##}, Missing[]], Mean[{##}], Complement[{##}, {Missing[]}]] &, {...

View answer