cancel
Showing results for 
Search instead for 
Did you mean: 

Archives Discussions

Peterp
Journeyman III

Calculate diff. of a matrix [X(2)-X(1) X(3)-X(2) ... X(n)-X(n-1)]

Hi,

i've an NxM matrix and i want i want to calculate the difference of that matrix e.g.

Matrix :  1 2 3

              4 5 6

              7 8 9

ResultMatrix: 2-1 3-2

                     5-4 6-5

                     8-7 9-8

 

What is the best way to do this?

                   

                      

 

0 Likes
2 Replies
dukeleto
Adept I

Hi Peter,

 

a gather array input with a normal output works just fine for this: I have higher order finite differences code working in exactly the same way.

For example:

 

kernel void compute_diff( double in_stream[], out double out_stream<> {

     // get the index of the out stream being treated

     int4 outpos = instance();

     int xpos = outpos.x; 

 

     // compute the finite difference

    out_stream = in_stream[xpos+1] - in_stream[xpos] ;

}

That's all there is to it; if you declare your receiving stream to be one shorter in length than the original matrix, you will have no problems with the last value. If both streams have the same length, you will need to deal with the last position in out_stream, either by calling the kernel on out_stream.domain() with the appropriate limit, or by using the kernel offset possibility in brook+1.4.

 

Regards,

 

Olivier

0 Likes

It seems there would be a good way to do this without using scatter or gather by rearranging your data.

0 Likes