JRR
JRR

Reputation: 6152

un-slicing numpy arrays

Given a sliced numpy array as follows:

b = [a[..., i] for i in a.shape[-1]]

What is the most simple way I can recreate a from b?

Something like:

for i in range(a.shape[-1]):
    c[..., i] = b[i]

Upvotes: 0

Views: 72

Answers (1)

user2357112
user2357112

Reputation: 281529

Instead of that list comprehension, your original operation should have been

b = numpy.rollaxis(a, axis=-1)

which produces a view of a as a new array instead of a list of arrays.

The reverse operation is

c = numpy.rollaxis(b, axis=0, start=b.ndim)

Upvotes: 1

Related Questions