function h = head(A,K)
%HEAD Get first rows of codistributed array.
%   
%   HEAD(X) displays the first eight rows of the codistributed array X in
%   the command window without storing a value.
%   
%   HEAD(X,K) displays up to K rows from the beginning of the codistributed array
%   X. If X contains fewer than K rows, then the entire array is
%   displayed.
%   
%   Y = HEAD(X) or Y = HEAD(X,K) returns the first eight rows, or up to K
%   rows, of the codistributed array X.
%    
%   See also: HEAD, CODISTRIBUTED/TAIL, CODISTRIBUTED.
%   
%   


%   Copyright 2016-2022 The MathWorks, Inc.

if nargin<2
    K = matlab.bigdata.internal.util.defaultHeadTailRows();
else
    % Check that k is a non-negative integer-valued scalar
    validateattributes(K, ...
        {'numeric'}, {'real','scalar','nonnegative','finite','integer'}, ...
        'head', 'k')
end
if K>size(A,1)
    % Not enough rows, so return everything
    tmp = A;
else
    subs = repmat({':'},1,ndims(A));
    subs{1} = matlab.internal.ColonDescriptor(1,1,K);
    tmp = subsref(A, substruct('()',subs));
end

% As of R2023a, head is also available for numeric arrays. It follows the
% same startegy to display and not set ANS when no output is requested.
if nargout==0
    disp(tmp);
else
    h = tmp;
end