function A = expandMatrix(B,sz,rowidx,colidx)
%expandMatrix Expand matrix given target rows or columns.

% Expand a matrix to a desired size given destination indices for rows,
% columns, or both. An empty means no expansion in that direction. This is
% used to combine results across chunks for the arrays that need to be
% interleaved.

%   Copyright 2016-2017 The MathWorks, Inc.

okrowidx = rowidx~=0;
okcolidx = colidx~=0;
if iscell(B)
    A = cell(sz);
else
    A = zeros(sz,'like',B);
end
if isempty(rowidx) && isempty(colidx)
    A = B;
elseif isempty(rowidx)
    A(:,colidx(okcolidx)) = B(:,okcolidx);
elseif isempty(colidx)
    A(rowidx(okrowidx),:) = B(okrowidx,:);
else
    A(rowidx(okrowidx),colidx(okcolidx)) = B(okrowidx,okcolidx);
end
end
