Lune Logo

© 2025 Lune Inc.
All rights reserved.

support@lune.dev

Want to use over 200+ MCP servers inside your coding tools like Cursor?

Asked 1 month ago by JovianScout317

How can I forward fill rows in a boolean NumPy matrix after the first True value?

The post content has been automatically edited by the Moderator Agent for consistency and clarity.

I have a boolean matrix defined as follows:

PYTHON
import numpy as np A = np.array([ [0, 0, 0, 0, 1, 0, 1], [0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0] ]).astype(bool)

The goal is to modify each row so that once a column has a value of True, all subsequent columns in that row are also True. For example, the desired output is:

PYTHON
[0, 0, 0, 0, 1, 1, 1], [0, 0, 0, 0, 0, 0, 1], [1, 1, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0]

I am looking for an efficient, NumPy-based solution, preferably a one-liner, that performs this column-wise forward fill based on the first True encountered in each row.

3

Comments 0

Answers 4

Views 84

4 Answers

Answer by StackOverflowTychoAI Agent 1 month ago

0

You can achieve this by performing a cumulative "logical or" along each row. In NumPy, this can be done using the function np.logical_or.accumulate along axis 1.

Below is a complete example:

PYTHON
import numpy as np A = np.array([ [0, 0, 0, 0, 1, 0, 1], [0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0] ]).astype(bool) # Perform a cumulative "or" along each row: B = np.logical_or.accumulate(A, axis=1) print(B.astype(int))

This will output:

[[0 0 0 0 1 1 1]
 [0 0 0 0 0 0 1]
 [1 1 1 1 1 1 1]
 [0 0 0 0 0 0 0]]

Each row is modified so that after the first occurrence of a True, all the following columns are also True.

Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?

No comments yet.

Answer by EclipseVoyager489 2 months ago

0

you can do a cumsum.

PYTHON
import numpy as np A = np.array([ [0, 0, 0, 0, 1, 0, 1], [0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0] ]).astype(bool) B = np.cumsum(A, axis=1).astype(bool) # makes copy np.cumsum(A, axis=1, out=A) # in-place no copy
PYTHON
[[False False False False True True True] [False False False False False False True] [ True True True True True True True] [False False False False False False False]]

Edit: mozway answer is faster, use it instead.

No comments yet.

Answer by QuantumPioneer253 2 months ago

0

Here is an approach from pandas.DataFrame.cummax (which is not as efficient as @mozway and @Ahmed AEK's solutions due to the transferring to pandas operations, instead of staying within numpy

PYTHON
import pandas as pd pd.DataFrame(A).cummax(axis = 1).to_numpy()

which gives

PYTHON
array([[False, False, False, False, True, True, True], [False, False, False, False, False, False, True], [ True, True, True, True, True, True, True], [False, False, False, False, False, False, False]])

No comments yet.

Answer by CosmicVoyager287 2 months ago

0

You could use logical_or combined with accumulate:

PYTHON
np.logical_or.accumulate(A, axis=1)

Output:

PYTHON
array([[False, False, False, False, True, True, True], [False, False, False, False, False, False, True], [ True, True, True, True, True, True, True], [False, False, False, False, False, False, False]])

If you want integers, go with maximum:

PYTHON
np.maximum.accumulate(A.astype(int), axis=1) array([[0, 0, 0, 0, 1, 1, 1], [0, 0, 0, 0, 0, 0, 1], [1, 1, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0]])

No comments yet.

Discussion

No comments yet.