function x = sendToDeviceIfPossible(x)
%SENDTODEVICEIFPOSSIBLE Copy input to GPU if it is numeric or logical
%   XGPU = sendToDeviceIfPossible(X) sends the array X to the GPU if it is
%   numeric or logical and not already a gpuArray.
%
%   TBLGPU = sendToDeviceIfPossible(TBL) sends the variable in the table
%   TBL to the the GPU if it is numeric or logical and not already a 
%   gpuArray.

%   Copyright 2020-2021 The MathWorks, Inc.

if istable(x)
    % Use for loop rather than varfun to preserve the properties of the
    % table (for example, x.Properties.VariableUnits).
    for ii = 1:width(x)
        x.(ii) = sendToDeviceIfPossible(x.(ii));
    end
elseif ~isgpuarray(x) && (isnumeric(x) || islogical(x))
    x = gpuArray(x);
end
end