% Returns sumB, the [n_storeys × n_storeys] global bracing stiffness matrix
% B = [n_storeys × n_cols] matrix of B(i,j) bracing stiffness at each node in the direction of sway
% sway_dir = "LEFT" or "RIGHT"; sway direction of the frame / direction of the braces
function [sumB] = Assemble_Bracing_Matrix(B,sway_dir)

    % Global bracing stiffness matrix
    n_storeys = size(B,1);
    n_cols = size(B,2);
    sumB = zeros(n_storeys,n_storeys); 
    sumBj = zeros(n_storeys,n_storeys);
    sumBpj = zeros(n_storeys,n_storeys);
    for j=1:n_cols
        
        % Populate Bj matrix
        Bj = zeros(n_storeys,n_storeys);
        for i = 1:n_storeys
            Bj(i,i) = B(i,j);
        end
        
        % Populate Bpj matrix
        Bpj = zeros(n_storeys,n_storeys);
        if sway_dir == "LEFT" && j>1
            for i =1:n_storeys-1
                Bpj(i,i+1) = B(i+1,j-1);
            end
        elseif sway_dir == "RIGHT" && j<n_cols
            for i =1:n_storeys-1
                Bpj(i,i+1) = B(i+1,j+1);
            end
        else
        end
        
        % Assemble global bracing stiffness matrix
        sumB = sumB + Bj - Bpj;
        sumBj = sumBj + Bj;
        sumBpj = sumBpj + Bpj;
    end
end