What would be the most efficient approach to perform a logical OR (on 1’s and 0’s) on every column of a matrix in Python?
0 0 0
0 0 0
1 0 1
0 0 1
OR 0 0 1
_ _ _
1 0 1
Some context: I am employing a dynamic programming method for generating a table for a small project I’v been working on. I have 2 tables, one is 3-d and the other is 2-d. In the 3-d table only has 1’s or 0’s in it and the other has contains ints.
The 2-d table is generated as the 3-d one is, so I have to interleave them. The reason I want to OR an entire column is see if there was any 1’s in it, if so the same column will contain a 1 in the 2-d table.
Example:
3-d table:
[1][i][j]
1000010000000000...
1000000001000000...
1000000000010000...
1000000000000001...
[2][i][j]
1000100000000000...
1000001000000000...
1000000001000000...
2-d table:
(after OR’ing each column of the 3-d table, where the left-most most indices corresponds to a row in the 2-d table):
1000010001100001...
100010100000000...
Not sure if this is all clear…but thank you to anyone who helps! 😀
I wont explain the method I used
How do you represent this data? If that’s lists of lists, then swap last two indexes of a 3D-array, so “columns” can be accessed as sequential lists, and use
any. That’s about as fast as you can get in vanilla CPython: loop will be completely executed in C runtime.