[Numpy-discussion] How to concatenate two arrayswithout duplicating memory?

Sebastian Haase seb.haase@gmail....
Wed Sep 2 04:58:15 CDT 2009


Hi,
depending on the needs you have you might be interested in my "minimal
implementation" of what I call a
mock-ndarray.
I needed somthing like this to analyze higher dimensional stacks of 2d
images and what I needed was mostly the indexing features of
nd-arrays.
A mockarray is initialized with a list of nd-arrays. The result is a
mock array having one additional dimention "in front".
>>> a = N.arange(9)
>>> b = N.arange(9)
>>> a.shape=3,3
>>> b.shape=3,3
>>> c = F.mockNDarray(a,b)
>>> c.shape
(2, 3, 3)
>>> c[2,2,2]
>>> c[1,2,2]
8

No memory copy is done.

I put the module file here
http://drop.io/kpu4bib/asset/mockndarray-py
Otherwise this is part of my (BSD) "Priithon" image analysis framework.

Regards
Sebastian Haase

On Wed, Sep 2, 2009 at 11:31 AM, "V. Armando Solé"<sole@esrf.fr> wrote:
> Citi, Luca wrote:
>> As Gaël pointed out you cannot create A, B and then C
>> as the concatenation of A and B without duplicating
>> the vectors.
>>
>> But you can still re-link A to the left elements
>> and B to the right ones afterwards by using views into C.
>>
>
> Thanks for the hint. In my case the A array is already present and the
> contents of the B array can be read from disk.
>
> At least I have two workarounds making use of your suggested solution of
> re-linking:
>
> - create the C array, copy the contents of A to it and read the contents
> of B directly into C with duplication of the memory of A during some time.
>
> - save the array A in disk, create the array C, read the contents of A
> and B into it and re-link A and B with no duplication but ugly.
>
> Thanks,
>
> Armando
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>


More information about the NumPy-Discussion mailing list