Ali Hashmi
  • Member for 6 years, 10 months
  • Last seen this week
Compile generates non-tensor object warning for this simple function
1 votes

partly based on xzczd's suggestion we can rewrite the function fn1 = Compile[{{sourcept, _Real, 1}, {ls, _Real, 3}}, Block[{temp, firstelem, vec, pos = 0}, (vec = #; firstelem = Compile`...

View answer
slow and jerky Graphics in 12.1.1 compared to older version 9.0
1 votes

This is not an answer but I think I found the culprit that is causing the kernel in 12.1.1 to crash. It is the kernel calling the OpenJDK Platform Binary. The same code (see the question) runs fine in ...

View answer
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
Find the values of the largest key of an association
1 votes

Function[x, x@*Max@AssociationMap[Reverse][x]]@assoc although I would use the method proposed by @kglr i.e. assoc[Max @ Keys @ assoc]

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
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
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
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
Remove terms from the list
1 votes

this should be quite fast: Pick[l, Unitize@l[[All, -1]], 1] (* {{-5, -5, -5}, {-7, -5, -1}, {-9, -3, 15}, {-11, 1, 49}} *)

View answer
intersection between two 2D arrays with labeled data is slow
1 votes

This approach works for the posted question as well as for cases when there are gaps in labels. What i mean by gaps is that the first or the second segmented mask may have a structure as follows: {1,2,...

View answer
How to extract a row name that meets a column name criterion in a matrix
Accepted answer
1 votes

two ways i can think of with your current m: g[{0, 0}] = {}; g[{1, 0}] = {"N1"}; g[{0, 1}] = {"N2"}; g[{1, 1}] = {"N1", "N2"}; Map[First@# -> g@Rest@# &, Rest@Thread[m]] // AbsoluteTiming ...

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
Delete one item in a list
1 votes

drop[x_][list_] := ReplacePart[list, FirstPosition[list, x] :> Sequence[]]

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
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
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
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
Removing elements of a specific length from a list
1 votes

Doing it using recursion: deleteRecursively[{}] = {}; deleteRecursively[{x_Integer}] := {x}; deleteRecursively[x : {_List ..}] := With[{crit = 9}, If[Length@First[x] == crit, Join[{Null}, ...

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
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
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
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
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
select points outside the cells in a lattice?
1 votes

Solution below should work for version 11.1.1 and earlier As @Carl Woll showed in his answer that the embedding dimension for region in question was 2 and not 3, so i replaced MeshRegion with ...

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
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
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
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
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
1
2 3 4 5 6