The construction of the impedance matrix is done by three different cases
Step 1. it stars with the inclusion of buses that have shunt elements plus the admittance in derivation with or without tap
Step 2. It adds the inclusion of new buses not added in Case 1 by adding the series impedance between an existing bus from case 1 and a new one.
Step 3. This step adds the values of the shunt elements and admittance in derivation with or without tap to those buses that were not added in Case 1.
Orlando Ramirez Barron (2020). Impedance matrix (https://www.mathworks.com/matlabcentral/fileexchange/69400-impedance-matrix), MATLAB Central File Exchange. Retrieved .