2
votes

I would like to find the index of the smallest value resulting from some computation, like the nearest value, using Matlab gpuArrays.

However, in the arrayfun scenario the min function doesn't seem to offer the functionality.

With the following code:

function grid_gpu_test
    gridSize = 8;
    grid = gpuArray(rand(gridSize));

    all_c=1:gridSize; % because : is not supported

    function X = min_diff(row)
        X = min(abs(grid(row,all_c)-grid(row,1)))
    end

    rows = gpuArray.colon(2, gridSize)';
    arrayfun(@min_diff, rows)
end

I get the following error:

Too few input arguments supplied to: 'min'. Error in 'grid_gpu_test' (line: 9)

Is there a way to achieve this? I know that using min(gpuArray) works normally when it's not in arrayfun, but I want to achieve this with an operation that doesn't simplify into matrix operations.

1

1 Answers

1
votes

I'm a little confused by your question, because your code errors out when you try to run it on the CPU. By making rows go 2:(gridSize+1), then it exceeds the size of grid.

In any case, I think here rather than arrayfun, you want to use bsxfun (or implicit expansion if you have R2016b or later). Here's the bsxfun version.

grid = gpuArray.rand(8);
% I think what you're trying to compute is the difference
% between each column of "grid" compared to the first column
difference = bsxfun(@minus, grid(:,1), grid);
% To find the minimum difference, and its column, use
% the following form of MIN
[val, col] = min(difference, [], 2)

Here I'm using the "reduction" form of min, and I want to reduce across columns, so I need to pass in the 2 as the third argument. The second argument is [] to tell MATLAB that you want the "reduction" form of min, rather than the element-wise form of min. (Note that gpuArray/arrayfun supports only the element-wise form of min, which explains the error you're seeing).

Based on the extra information in the comments, perhaps xcorr2 is what you're after (this works on the GPU).