function Z = internal_stridedadd(X, strideX, Y, strideY)
% This function is undocumented and may change in a future release.

%   Z = internal_stridedadd(X, strideX, Y, strideY)
%   Compute the addition X(1:strideX(1):end, 1:strideX(2):end, ...) + 
%       Y(1:strideY(1):end, 1:strideY(2):end, ...).
%    Valid for stride factors that contain up to 3 elements (i.e., striding in
%    at most 3 dimensions).
%
%   Inputs:
%     X               numeric array
%     strideX         vector of positive integers
%     Y               numeric array
%     strideY         vector of positive integers
%
%   Outputs:
%     Z               numeric array

%   Copyright 2022 The MathWorks, Inc.

Z = iApplyStrides(X, strideX) + iApplyStrides(Y, strideY);

end

function X = iApplyStrides(X, stride)
% Downsample an array using given striding factors.
if all(stride == 1) || isempty(stride)
    return;
end
n = numel(stride);
if n == 2
    X = X(1:stride(1):end, 1:stride(2):end, :, :, :);
elseif n == 1
    X = X(1:stride(1):end, :, :, :);
else
    X = X(1:stride(1):end, 1:stride(2):end, 1:stride(3):end, :, :, :);
end
end