Skip to main content
added 538 characters in body
Source Link
Mr.Wizard
  • 275.2k
  • 34
  • 606
  • 1.5k

I recommend using Pick for these things. It has already been used by others but the simplest form using Alternatives hasn't been shown:

Pick[#, #[[All, 1]], Alternatives @@ #2] &[mymatrix, compvector] 

{{1, 1, -56}, {1, 2, 3.06}, {2, 0, -30.02}, {6, 1, 9.945}, {7, 0, -7.512}}

For greater speed especially with longer comvectors we can use a Dispatch table:

fast[m_, c_] := Pick[m, m[[All, 1]] /. Dispatch @ Thread[c -> True]] fast[mymatrix, compvector] 

{{1, 1, -56}, {1, 2, 3.06}, {2, 0, -30.02}, {6, 1, 9.945}, {7, 0, -7.512}}

Timings with some larger data:

mymatrix = RandomInteger[9999, {50000, 3}]; compvector = RandomChoice[Range@9999, 150]; (* the faster of Michael's functions *) michael[m_, c_] := With[{nf = Nearest[c]},  Pick[m, # - First /@ nf /@ # &@m[[All, 1]], 0] ] michael2[m_Cases[mymatrix, c_] :={x_, Pick[m_, Times_} @@@/; Outer[PlusMemberQ[compvector, m[[All,x]] 1]],// -c],Timing 0] // First Pick[#, #[[All, 1]], Alternatives @@ #2] &[mymatrix, compvector] // Timing // First Cases[mymatrixmichael[mymatrix, {x_,compvector] _, _} /; MemberQ[compvector, x]] // Timing // First michael[mymatrix, compvector]  // Timing // First michael2[mymatrixfast[mymatrix, compvector]  // Timing // First 

03.094447

0.39905

0.094265

0.765047

The length of compvector was chosenA run-off with care; shorter vectors favor my method, while longer ones favor Michael's first function.method on even larger data:

mymatrix = RandomInteger[99999, {500000, 3}]; compvector = RandomChoice[Range@99999, 15000]; michael[mymatrix, compvector] // Timing // First fast[mymatrix, compvector] // Timing // First 

15.943

0.327

I recommend using Pick for these things. It has already been used by others but the simplest form using Alternatives hasn't been shown:

Pick[#, #[[All, 1]], Alternatives @@ #2] &[mymatrix, compvector] 

{{1, 1, -56}, {1, 2, 3.06}, {2, 0, -30.02}, {6, 1, 9.945}, {7, 0, -7.512}}

Timings with some larger data:

mymatrix = RandomInteger[9999, {50000, 3}]; compvector = RandomChoice[Range@9999, 150]; michael[m_, c_] := With[{nf = Nearest[c]}, Pick[m, # - First /@ nf /@ # &@m[[All, 1]], 0] ] michael2[m_, c_] := Pick[m, Times @@@ Outer[Plus, m[[All, 1]], -c], 0]  Pick[#, #[[All, 1]], Alternatives @@ #2] &[mymatrix, compvector] // Timing // First Cases[mymatrix, {x_, _, _} /; MemberQ[compvector, x]] // Timing // First michael[mymatrix, compvector] // Timing // First michael2[mymatrix, compvector] // Timing // First 

0.094

0.39

0.094

0.765

The length of compvector was chosen with care; shorter vectors favor my method, while longer ones favor Michael's first function.

I recommend using Pick for these things. It has already been used by others but the simplest form using Alternatives hasn't been shown:

Pick[#, #[[All, 1]], Alternatives @@ #2] &[mymatrix, compvector] 

{{1, 1, -56}, {1, 2, 3.06}, {2, 0, -30.02}, {6, 1, 9.945}, {7, 0, -7.512}}

For greater speed especially with longer comvectors we can use a Dispatch table:

fast[m_, c_] := Pick[m, m[[All, 1]] /. Dispatch @ Thread[c -> True]] fast[mymatrix, compvector] 

{{1, 1, -56}, {1, 2, 3.06}, {2, 0, -30.02}, {6, 1, 9.945}, {7, 0, -7.512}}

Timings with some larger data:

mymatrix = RandomInteger[9999, {50000, 3}]; compvector = RandomChoice[Range@9999, 150]; (* the faster of Michael's functions *) michael[m_, c_] := With[{nf = Nearest[c]},  Pick[m, # - First /@ nf /@ # &@m[[All, 1]], 0] ] Cases[mymatrix, {x_, _, _} /; MemberQ[compvector, x]] // Timing // First Pick[#, #[[All, 1]], Alternatives @@ #2] &[mymatrix, compvector] // Timing // First michael[mymatrix, compvector]   // Timing // First fast[mymatrix, compvector]  // Timing // First 

3.447

0.905

0.265

0.047

A run-off with Michael's method on even larger data:

mymatrix = RandomInteger[99999, {500000, 3}]; compvector = RandomChoice[Range@99999, 15000]; michael[mymatrix, compvector] // Timing // First fast[mymatrix, compvector] // Timing // First 

15.943

0.327

Source Link
Mr.Wizard
  • 275.2k
  • 34
  • 606
  • 1.5k

I recommend using Pick for these things. It has already been used by others but the simplest form using Alternatives hasn't been shown:

Pick[#, #[[All, 1]], Alternatives @@ #2] &[mymatrix, compvector] 

{{1, 1, -56}, {1, 2, 3.06}, {2, 0, -30.02}, {6, 1, 9.945}, {7, 0, -7.512}}

Timings with some larger data:

mymatrix = RandomInteger[9999, {50000, 3}]; compvector = RandomChoice[Range@9999, 150]; michael[m_, c_] := With[{nf = Nearest[c]}, Pick[m, # - First /@ nf /@ # &@m[[All, 1]], 0] ] michael2[m_, c_] := Pick[m, Times @@@ Outer[Plus, m[[All, 1]], -c], 0] Pick[#, #[[All, 1]], Alternatives @@ #2] &[mymatrix, compvector] // Timing // First Cases[mymatrix, {x_, _, _} /; MemberQ[compvector, x]] // Timing // First michael[mymatrix, compvector] // Timing // First michael2[mymatrix, compvector] // Timing // First 

0.094

0.39

0.094

0.765

The length of compvector was chosen with care; shorter vectors favor my method, while longer ones favor Michael's first function.