%***********************************************************************
%  function MergeElemIntoMasterStiff
%
%  Merge a 4x4 bar element stiffness into the global stiffness matrix
%***********************************************************************

function K = MergeElemIntoMasterStiff(Ke, eftab, K)

% eftab =  element freedom table: maps element dofs to global dofs

for idof = 1:4
    irow = eftab(idof);
    for jdof = 1:4
        jcol = eftab(jdof);
        K(irow, jcol) = K(irow, jcol) + Ke(idof,jdof);
    end
end
        
% Test code and output:
%
% K = zeros(6)
%
% K =
%     0     0     0     0     0     0
%     0     0     0     0     0     0
%     0     0     0     0     0     0
%     0     0     0     0     0     0
%     0     0     0     0     0     0
%     0     0     0     0     0     0
%
% Ke = ElemStiff2DTwoNodeBar(0, 0, 10, 10, 100, 2*sqrt(2))
%
% Ke =
%   10.0000   10.0000  -10.0000  -10.0000
%   10.0000   10.0000  -10.0000  -10.0000
%  -10.0000  -10.0000   10.0000   10.0000
%  -10.0000  -10.0000   10.0000   10.0000
%
% K = MergeElemIntoMasterStiff(Ke, [1,2,5,6], K)
%
% K =
%   10.0000   10.0000         0         0  -10.0000  -10.0000
%   10.0000   10.0000         0         0  -10.0000  -10.0000
%         0         0         0         0         0         0
%         0         0         0         0         0         0
%  -10.0000  -10.0000         0         0   10.0000   10.0000
%  -10.0000  -10.0000         0     