Tag Info

New answers tagged

2

Apologies if I've misunderstood your questions. I've never used Dispatch before, as suggested by Kuba in the comments, but it seems to be the right tool for the problem if speed is your concern. First off, I hope it's clear that accessing the list corresponding to an index pair is as straightforward is applying the rules list for pattern replacement: rules ...


7

Reposting my answer from here (its relevant part about SparseArray) The anatomy of sparse arrays We start with a generally useful API for construction and deconstruction of SparseArray objects: ClearAll[spart, getIC, getJR, getSparseData, getDefaultElement, makeSparseArray]; HoldPattern[spart[SparseArray[s___], p_]] := {s}[[p]]; getIC[s_SparseArray] := ...


8

This is a bug in Pick caused by SparseArray, has nothing to do with Graph. Minimal example (SparseArray object is the fullform version of your vLM): x = {1, 2, 3, 4, 5, 6}; Pick[x, SparseArray[Automatic, {6}, 0, {1, {{0, 3}, {{2}, {3}, {4}}}, {1, 1, 1}}], 0]; FullForm@x {1, System`Private`InternSequence[], System`Private`InternSequence[], ...


3

This looks like a bug to me. Here is a slightly more minimal example. ue = UndirectedEdge; g = Graph[ue @@@ {{1, 2}, {2, 3}, {1, 3}, {1, 4}, {4, 5}, {4, 6}}]; vl = VertexList[g] aM = AdjacencyMatrix[g]; vLM = aM[[VertexIndex[g, 1]]]; Pick[vl, vLM, 0]; VertexList[g] Output {1, 2, 3, 4, 5, 6} {1, 5, 6} You can solve the error by making a copy of the ...


6

Answering my own question per Szabolcs' sugggestion: Answer WRI affirms that SparseArray[__] is broken (in 9.0 and 9.01) WRI's suggested workaround is SparseArray//Normal My experience with this particular SparseArray bug has been that reliable coding requires that the Normal workaround be applied to every instance of SparseArray, because the failures ...


3

It occurred to me that this problem can be recast as an image processing one. I think this approach is different enough to warrant its own answer. I like the style much better. f3[array_List, ele_, dist_] := Image[array] ~Binarize~ {ele, ele} ~MaxFilter~ {0, dist} // Join @@ Pick[array, ImageData @ #, 1] & f3[ar, 5, 2] {10, 6, 5, 7, 3, 7, 5, ...


2

I got an error running your code in version 7 and I had to use FromDigits[Normal@#, 2] to fix it. Therefore I don't know if my comparative timings are meaningful but here is what I came up with: f2[array_, ele_, dist_] := SparseArray[Unitize[array - ele], Automatic, 1]["AdjacencyLists"] // Outer[Plus, Range[-dist, dist], #, 1] & // ...



Top 50 recent answers are included