function [X, sequenceLengths] = padSequences(sequences, paddingValue)
%PADSEQUENCES Pad sequences with value.
% [X, sequenceLengths] = padSequences(sequences, paddingValue) right-pads
% the sequences with padding value to the length of the longest sequence
% and returns the unpadded sequence lengths.

% Initialize mini-batch with padding.
numObservations = size(sequences,1);
sequenceLengths = cellfun(@(x) size(x,2), sequences);
maxLength = max(sequenceLengths);
X = repmat(paddingValue, [1 numObservations maxLength]);

% Insert sequences.
for n = 1:numObservations
    X(1,n,1:sequenceLengths(n)) = sequences{n};
end

end

