0

Let's say I have the following matrices:

A = [[a row with a lot of numbers],[another row with a lot of numbers], ...]
B = [[num1, num2, num3, num4],[num5, num6, num7, num8], ..., other rows with 4 numbers]

What would be an efficient way to loop through all rows in A checking if:

  • num1 and num2 are consecutively in this row
    and
  • num3 and num4 are consecutively in this row

or

  • num5 and num6 are consecutively in this row
    and
  • num7 and num8 are consecutively in this row

or... (the same check with every other row in B)

If some of these 'or' happens, return True.

I've managed to do these checks by looping through B for every element in A, but since A is a very long matrix, it's taking too long to run, because I have 4 for loops, one inside of the other. Is there a more efficient way?

Kobayashi
  • 119
  • 3
  • 10
  • 1
    How often is `A` updated or changed relative to how often you need to make the query? Preprocessing `A` into set of adjacent pairs would be worthwhile if you need to make a lot of queries against a single instance of `A`. – chepner Apr 27 '18 at 17:05
  • This [other question](https://stackoverflow.com/questions/49993101/python-numpy-vectorize-nested-for-loops-for-combinatorics) will probably help, or even answer the question – OriolAbril Apr 27 '18 at 17:09
  • 1
    Extending chepner's suggestion, you can do the same thing with B, if A changes often but B doesn't. – Arndt Jonasson Apr 27 '18 at 17:56

0 Answers0