function out = assignSlices(out, indices, in, dim)
% ASSIGNSLICES  Assign slices of an N-D array
% out(:, :, ..., indices, :, :, ...) = in; where indices is the dim'th argument.

%   Copyright 2021 The MathWorks, Inc.

inputIsCodist = cellfun(@iscodistributed, {in, indices, out});
if any(inputIsCodist)
    inputIndex = find(inputIsCodist, 1);
    switch inputIndex
        case 1
            codistributed.pVerifyUsing1d('assignSlices', in);
        case 2
            codistributed.pVerifyUsing1d('assignSlices', indices);
        case 3
            codistributed.pVerifyUsing1d('assignSlices', out);
    end
end
subsArg = repmat({':'}, 1, max(dim,ndims(out)));
subsArg(dim) = {indices};
subs = substruct('()', subsArg);
out = subsasgn(out, subs, in);
end