I would like to subtract 2 arrays. The first array is in groups of three. From each of these groups, I would like to do element-wise subtraction and summing of the results. The result element should include 1 element for each input group. The following example demonstrates what I am looking for. I would use a for-loop, but the operations which generate these values are nominally done on the GPU; I would like to include the following operation as gpu-enabled as well. At minimum, I would like to "flip" the dimension on which the loop iterates. Resizing the main array is not ideal, because it would need to be restored for the next iteration of the algorithm (of which there are millions).
yt = [ 1;1;1 ; 2;2;2 ; 3;3;3 ; 4;4;4 ; 5;5;5 ; 6;6;6 ; 7;7;7 ];
at = [ 1;1;1 ];
No = numel(at);
Np = numel(yt) / No;
for i = 1 : Np
lb = (i-1)*No + 1;
ub = i*No;
f(i) = sum( yt(lb:ub) - at(lb:ub) );
end
f = [ 0 ; 3 ; 6 ; 9 ; 12 ; 15 ; 18 ]