How to sum up specific layers of a 3d wave?

Hi,
I have a 3d wave witch contains images (512x512) and the names of these images end with to “pos”, “neg”, “prepos” or “preneg”. Now I like to add every image which ends to “pos”, to “neg” or to one of the others.
My first straightforward attempt was the following:
            for (ilayer = 0; ilayer < nlayers; ilayer += 1)
            //select images with matching criteria take1 = “pos”
            if( GrepString(wstackfiles[ilayer], take1) )
            addShiftedImage(wcoadd1,wstack,ilayer,0,0)
            normalization1+=1
            endif
           
            if( GrepString(wstackfiles[ilayer], take2) )
            addShiftedImage(wcoadd2,wstack,ilayer,0,0)
            normalization2+=1
            endif  
            if( GrepString(wstackfiles[ilayer], take3) && strlen(take3) > 0 )
            addShiftedImage(wcoadd3,wstack,ilayer,0,0)
            normalization3+=1
            endif
            if( GrepString(wstackfiles[ilayer], take4)  && strlen(take4) > 0 )
              addShiftedImage(wcoadd4,wstack,ilayer,0,0)
              normalization4+=1
            endif
               endfor

With
Function addShiftedImage(wcoadd1,wstack,ilayer,pdrift,qdrift)
    …
     wcoadd1 += wstack[p+pdrift][q+qdrift][ilayer]

End

But this is slow, for 40 images it takes around 6 seconds. The main time consuming process is the addShiftedImage. Therefore I thougt it should be possible to speed it up by use of MatrixOp and/or MulthiThread.
If I use MatrixOp Funtions, I can speed up by a factor of 3:
    for (ilayer = 0; ilayer < nlayers; ilayer += 1)
        if( GrepString(wstackfiles[ilayer], take1) )
            MatrixOP/NTHR=0/O/Free temp = wcoadd1 + wstack[][][ilayer]
            wcoadd1 = temp
            normalization1+=1
            endif

        if( GrepString(wstackfiles[ilayer], take2) )
            MatrixOP/NTHR=0/O/Free temp2 = wcoadd2 + wstack[][][ilayer]
            wcoadd2 = temp2
            normalization2+=1
        endif
            if( GrepString(wstackfiles[ilayer], take3) && strlen(take3) > 0 )
            MatrixOP/NTHR=0/O/Free temp3 = wcoadd3 + wstack[][][ilayer]
            wcoadd3 = temp3
            normalization3+=1
            endif
            if( GrepString(wstackfiles[ilayer], take4)  && strlen(take4) > 0 )
                MatrixOP/NTHR=0/O/Free temp4 = wcoadd4 + wstack[][][ilayer]
                wcoadd4 = temp4
                normalization4+=1
            endif
        endfor

Now, I think it should be possible to speed it more up by Multithreading and to start a thread for each “pos”, “neg”…
Is this possible?
Thanks,

-Why do you use a dfferent temporary wave for each MatrixOp call? I would use the same free wave in all cases, created with the correct size before the loop. In that way you can avoid any memory allocations in the loop.
-As fas as I understand your code each image can only be one of take1, take2, ...? If yes use one if/endif with elseif.
-For even more speedup you might try to rearrange wstack before so that all take? images are adjacent, then you could use the syntax
wave3d[][][a,b] of MatrixOp to sum then up in one call.

What is your target layer number?
Hi Thomas,

rearrangewas a great idea! In total I reduced the compuation time from 6s to around 0.1s!

Thanks a lot,
To sum up with the commant 3dWave[][][a,b] gives different results. It does not add the point (0,0) from layer a to layber b, for example. I have to sum the z-values of the layers a to b point by point.