Reputation: 1592
I have a matrix
m = np.zeros((5,5))
and what to have a view on the last row
row = m[-1]
however if I add a column to m:
m = np.c_[m[:,:2],[0,0,1,1,1],m[:,2:]]
and print out row I don't get the new column.
Is there any way to get the change without use the line
row = m[-1]
again?
Upvotes: 1
Views: 189
Reputation: 38982
What you want to achieve here isn't currently possible within the bounds of the numpy
library.
See this answer on numpy arrays occupying a contiguous block of memory.
You can use the rx
library to get around this by making a subject of the last row.
import numpy as np
from rx import Observable, Observer
from rx.subjects import Subject
m = np.zeros((5,5))
m_stream = Subject()
last_row_stream = Subject()
last_row = None
def update_last(v):
global last_row
last_row = v
last_row_stream.subscribe(on_next=update_last)
last_row_stream.on_next(m[-1])
print(last_row)
m_stream.map(
lambda v: np.insert(v, 2, [0, 0, 1, 1, 1], axis=1)
).subscribe(lambda v: last_row_stream.on_next(v[-1]))
m_stream.on_next(m)
print(last_row)
Upvotes: 1