Good day.

I have a problem with matrices / arrays, i cannot resolve. I have no idea how to start...

I have two 2D matrices and i want to overlap (mix?) one over the other, no matter what dimensions they have.

For example :

a = [ (1,2,3,4,5),
(9,7),
(0,0,0,0,0),
(5,5,5) ]

b = [ (0,0,0),
(1,1,1,1),
(2,2,2,2) ]

Now, if i want to put a over b, i get the result:

a over b = [
(1,2,3,4,5),
(9,7,1,1),
(0,0,0,0,0),
(5,5,5,2) ]

And b over a = [
(0,0,0,4,5),
(1,1,1,1),
(0,0,0,0,0),
(2,2,2,2) ]

I think this operation has a name in mathematics, but i don't know how to call it... and what to search for.
I tried 2 days to make a good algorithm and i gave up... Please keep in mind that my matrices are not square or rectangle. They can be very nasty to combine.
In fact, there is an undefined number of matrices i need to overlap, all this overlaping is in a strict order. I suppose that if i have the algorithm for 2 matrices, i can make a big "for" cicle and mix the next matrix over the previous result.
I need this overlap to happen AS FAST AS POSSIBLE. I need to overlap matrices with milions of elements, like 1600x1200.

Any help is welcome. Thank you very much.

Sorry. Mistake.
Correct b over a is
[ (0,0,0,4,5),
(1,1,1,1),
(2,2,2,2,0),
(5,5,5) ]

Alright, here's some psuedo code for how I did it. I had two functions. First I made one to handle each row:

def handle_row(row_a, row_b):
    new_row = []
    Get the length of each row
    compute the difference in length
    if diff > 0:
        extend row_b by diff
    else:
        extend row_a by the absolute value of diff
    for each_item in row_a:
        append the bigger of the two values (row_a[idx], row_b[idx])
    return new_row

The main function acts in almost exactly the same way, except you use handle_row isntead of the max evaluation

EDIT: Now I see that you're not evaluating based on value... simply replacing values that exist... either way, the psuedo code should help you get started. When you run into hiccups post your relevant code and error traceback and we'll help you soldier on

Thank you so much!
This is my code.

#MY DATA IS vLmgl, is a dictionary containing classes that have a .data variable. This data variable is a list of numpy arrays. The numpy arrays are what we need.

vLmgl = { 'index1':<type 'instance'>, 'index2':<type 'instance'>, etc... }
TempA = [np.empty(0,'U')]

for elem in sorted(vLmgl.values(), key=sort_zorder):
    Data = elem.data    # This is a list of numpy ndarrays.
    #
    NData = Data[nr_row]           # New data, to be written over old data.
    OData = TempA[nr_row:nr_row+1] # This is old data.
    #
    if len(NData) >= len(OData):
        # TempB = New data.
        TempB = NData
    else: # If old data is longer than new data.
        # TempB = old data, resized to New length, or = with empty Unicode array of New length.
        TempB = np.resize( OData, len(NData) )
        TempB.put( (0,len(NData)-1), NData )
    #
    TempA[nr_row:nr_row+1] = [TempB] # Save replaced row as new, or overwrite existing.
    #
    del TempB
    #
#

Maybe anyone will find it useful. :)
If no-one can provide a better solution, you can consider this post SOLVED.
Thanx again. :)

I need this overlap to happen AS FAST AS POSSIBLE. I need to overlap matrices with milions of elements, like 1600x1200.

Don't use straight python for this. Try numpy or write a C extension with Cython or the C API if speed is of utmost importance.

This article has been dead for over six months. Start a new discussion instead.