[Numpy-discussion] Overlap arrays with "transparency"

josef.pktd at gmail.com josef.pktd at gmail.com
Mon May 18 11:50:50 EDT 2009


On Mon, May 18, 2009 at 11:38 AM, Michael S. Gilbert
<michael.s.gilbert at gmail.com> wrote:
> On Mon, 18 May 2009 05:37:09 -0700 (PDT), Cristi Constantin wrote:
>> Good day.
>> I am working on this algorithm for a few weeks now, so i tried almost everything...
>> I want to overlap / overwrite 2 matrices, but completely ignore some values (in this case ignore 0)
>> Let me explain:
>>
>> a = [
>> [1, 2, 3, 4, 5],
>> [9,7],
>> [0,0,0,0,0],
>> [5,5,5] ]
>>
>> b = [
>> [0,0,9,9],
>> [1,1,1,1],
>> [2,2,2,2] ]
>>
>> Then, we have:
>>
>> a over b = [
>> [1,2,3,4,5],
>> [9,7,1,1],
>> [1,1,1,1,0],
>> [5,5,5,2] ]
>>
>> b over a = [
>> [0,0,9,9,5],
>> 1,1,1,1],
>> 2,2,2,2,0],
>> 5,5,5] ]
>>


If you can convert the list of lists to a common rectangular shape
(masking missing values or assigning nans), then
conditional overwriting is very easy, something like

mask = a>0
a[mask] =b[mask]

but for lists of lists with unequal shape, there might not be anything
faster than looping.

Josef



More information about the NumPy-Discussion mailing list