Message Boards Message Boards

0
|
4786 Views
|
7 Replies
|
7 Total Likes
View groups...
Share
Share this post:

[?] Count the total presence of this group in a sequence?

hello!

I have a group of numbers:

group={05, 60, **13, 58**, 47}

I want to count the total presence of this group in a sequence:

seq={78, **13**, 38, 65, 90, 25, 52, 77, 12, 33, **58**, 83, 20, 45, 70, 7, 32, 57}

Total[SequenceCount[seq, {#}] & /@ group]

out:

2

now I have a group of 6 sub-sequences, I want to count the total presence of the first group in each sub-sequence:

subseq={{20, 3, 76, 17, 90, 73, 14, 87, 70, 65, 48, 31, 62, 45, 28, 59, 42, 25}, {48, 83, 28, 55, 90, 35, 62, 7, 42, 3, 38, 73, 10, 45, 80, 17, 52, 87}, {38, 59, 80, 69, 90, 21, 10, 31, 52, 83, 14, 35, 24, 45, 66, 55, 76, 7}, {14, 85, 66, 19, 90, 71, 24, 5, 76, 59, 40, 21, 64, 45, 26, 69, 50, 31}, {40, 33, 26, 7, 90, 83, 64, 57, 50, 85, 78, 71, 52, 45, 38, 19, 12, 5}, {78, 13, 38, 65, 90, 25, 52, 77, 12, 33, 58, 83, 20, 45, 70, 7, 32, 57}}

(in fact my list of sub-sequences is 3240 items)

I tried with PART and EXTRACT but it does not work Â…

if I manually enter PART # or EXTRACT # it works:

Total[SequenceCount[subseq[[6]], {#}] & /@ group]

Total[SequenceCount[Extract[subseq, 6], {#}] & /@ group]

out:

2

2

but if I attribute to the # sub-sequence list, it does not work:

Total[SequenceCount[subseq[[#]]&/@Range[6], {#}] & /@ group]

or

Total[SequenceCount[Extract[subseq, #]&/@Range[6], {#}] & /@ group]

out:

0

0

what I want to get is this:

{0, 0, 0, 1, 1, 2}

which are the presence of the 5 GROUP numbers in each 6 SUBSEQ sub-sequence

Can anyone help me?

tnk's

POSTED BY: Mutatis Mutandis
7 Replies
Posted 6 years ago

Does this answer your last question ok?

Table[Length[Intersection[newseq[[q]], newgroup[[i]]]], {i, 1, 
  Length[newgroup]}, {q, 1, Length[newseq]}]
POSTED BY: Paul Cleary

WORK!

POSTED BY: Mutatis Mutandis
Posted 6 years ago

Alternatively: Table[Length[Intersection[s, g]], {g, newgroup}, {s, newseq}]; no need to use Part[].

POSTED BY: J. M.

another questionÂ…

now that I have obtained the sequence with the best presence of the 5 numbers, by calculation, I get 24 new sequences. would it be possible to repeat the operation of "COUNT", of 11 new groups containing 5 numbers, in the 24 new sequences, with a single operation?

newgroup={{10,53,28,05,67}, {20,33,65,50,87}, {75,14,43,03,15}, {62,33,02,44,85}, {40,17,31,62,09}, {09,40,32,20,90}, {19,35,60,37,71}, {81,56,07,69,03}, {03,53,59,47,12}, {89,23,36,08,62}, {43,80,19,51,57}}

to check in new sequences:

newseq={{77,88,9,22,33,44,57,68,79,32,43,54,67,78,89,12,23,34}, {19,38,57,14,33,52,9,28,47,64,83,12,59,78,7,54,73,2}, {25,68,21,80,33,76,45,88,41,70,23,66,35,78,31,90,43,86}, {41,88,45,76,33,80,21,68,25,86,43,90,31,78,35,66,23,70}, {47,28,9,52,33,14,57,38,19,2,73,54,7,78,59,12,83,64}, {79,68,57,44,33,22,9,88,77,34,23,12,89,78,67,54,43,32}, {61,68,75,26,33,40,81,88,5,16,23,30,71,78,85,36,43,50}, {35,58,81,10,33,56,75,8,31,80,13,36,55,78,11,30,53,76}, {59,88,27,4,33,62,39,68,7,14,43,72,49,78,17,84,23,52}, {67,8,39,2,33,64,27,58,89,22,53,84,47,78,19,72,13,44}, {17,58,9,82,33,74,57,8,49,62,13,54,37,78,29,12,53,4}, {49,8,57,74,33,82,9,58,17,4,53,12,29,78,37,54,13,62}, {89,58,27,64,33,2,39,8,67,44,13,72,19,78,47,84,53,22}, {7,68,39,62,33,4,27,88,59,52,23,84,17,78,49,72,43,14}, {31,8,75,56,33,10,81,58,35,76,53,30,11,78,55,36,13,80}, {5,88,81,40,33,26,75,68,61,50,43,36,85,78,71,30,23,16}, {37,38,39,32,33,34,27,28,29,82,83,84,77,78,79,72,73,74}, {85,8,21,20,33,46,45,58,71,40,53,66,65,78,1,90,13,26}, {11,28,45,16,33,50,21,38,55,56,73,90,61,78,5,66,83,10}, {1,38,75,86,33,70,81,28,65,46,83,30,41,78,25,36,73,20}, {65,28,81,70,33,86,75,38,1,20,73,36,25,78,41,30,83,46}, {55,38,21,50,33,16,45,28,11,10,83,66,5,78,61,90,73,56}, {71,58,45,46,33,20,21,8,85,26,13,90,1,78,65,66,53,40}, {29,28,27,34,33,32,39,38,37,74,73,72,79,78,77,84,83,82}}

tnk's

POSTED BY: Mutatis Mutandis
Posted 6 years ago

In this case, using Table[] and Sum[] instead of Map[] makes for less confusing code:

Table[Sum[SequenceCount[s, {g}], {g, group}], {s, subseq}]

Alternatively, you can use a single Table[] and apply Total[] afterwards:

Total[Table[SequenceCount[s, {g}], {s, subseq}, {g, group}], {2}]
POSTED BY: J. M.

When nesting pure function you had better give distinctive names to the variables, to avoid conflict. For example

group = {05, 60, 13, 58, 47};
countInSequence[mySequence_] := 
  Total[Function[element, SequenceCount[mySequence, {element}]] /@ 
    group];
Map[countInSequence, subseq]
POSTED BY: Gianluca Gorni

TNK'S 4 YOUR BEST SOLUTION "sei sempre er mejo"

POSTED BY: Mutatis Mutandis
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract