h = [0.8,1.00,1.20,1.40,1.6,1.80];
Lss = [2.00,2.40,2.80,3.20,3.60,4.00];
Var1HeightofTrussBeam = h;
Var2LengthSpecialSegment = Lss;
Var3NumberSpecialSegment = Nss;
Variantmodel{ijk,:} = table(ijk,h(i),Lss(j),Nss(k),'VariableNames',{'VariantID','Height','Length','Number'});
Xbase{ijk,:} = L*(0:NumberBay);
Zbase{ijk,:} = repelem(0,1,length(Xbase{ijk,:}));
Xcolumn = L*(0:NumberBay);
Zcolumn = repelem(H*(1:NumberStory),1,length(Xcolumn));
Xbotbeam = L*(0:NumberBay);
Zbotbeam = repelem((H*(1:NumberStory)-h(i)),1,length(Xbotbeam));
idx = unique(Zbotbeam(:).');
Zbottombeam{ijk,:} = idx;
Xcol{ijk,:} = repmat([Xcolumn,Xbotbeam],1,NumberStory);
Zcol{ijk,:} = [Zcolumn,Zbotbeam];
totalspan{ijk,:} = los*Nos*2+Lss(j);
if totalspan{ijk,:} == 15.000
checkspan{ijk,:} = "correct span";
checkspan{ijk,:} = "span must be 15m";
XTopOrdinarySegment = reshape([los*(1:Nos),(L/2+Lss(j)/2)+los*(0:Nos-1)].'+L*(0:(NumberBay-1)),1,[]);
XBottomOrdinarySegment = XTopOrdinarySegment;
ZTopOrdinarySegment = repelem(H*(1:NumberStory),1,2*Nos*NumberBay);
ZBottomOrdinarySegment = repelem(H*(1:NumberStory)-h(i),1,2*Nos*NumberBay);
Xos{ijk,:} = repmat([XTopOrdinarySegment,XBottomOrdinarySegment],1,NumberStory);
Zos{ijk,:} = [ZTopOrdinarySegment,ZBottomOrdinarySegment];
XTopSpecialSegment = reshape((lss*(0:Nss(k))+L/2-Lss(j)/2).'+L*(0:(NumberBay-1)),1,[]);
XBottomSpecialSegment = XTopSpecialSegment;
ZTopSpecialSegment = repelem(H*(1:NumberStory),1,length(XTopSpecialSegment));
ZBottomSpecialSegment = repelem(H*(1:NumberStory)-h(i),1,length(XBottomSpecialSegment));
Xss{ijk,:} = repmat([XTopSpecialSegment,XBottomSpecialSegment],1,NumberStory);
Zss{ijk,:} = [ZTopSpecialSegment,ZBottomSpecialSegment];
X{ijk,:} = [Xbase{ijk,:},Xcol{ijk,:},Xos{ijk,:},Xss{ijk,:}];
Z{ijk,:} = [Zbase{ijk,:},Zcol{ijk,:},Zos{ijk,:},Zss{ijk,:}];
scatter(X{ijk,:},Z{ijk,:},'filled'),grid on
tangent = (Lss(j)/Nss(k))/h(i);
anglespecialsegment(ijk) = atand(tangent);
jointbase{ijk} = table((1:numel(Xbase{ijk,:}))',Xbase{ijk,:}',Zbase{ijk,:}','VariableNames',{'ID','X','Z'});
jointcolumn{ijk} = table((max(jointbase{ijk}.ID)+1:numel(Xcol{ijk,:})+max(jointbase{ijk}.ID))',Xcol{ijk,:}',Zcol{ijk,:}','VariableNames',{'ID','X','Z'});
jointordinarysegment{ijk} = table((max(jointcolumn{ijk}.ID)+1:numel(Xos{ijk,:})+max(jointcolumn{ijk}.ID))',Xos{ijk,:}',Zos{ijk,:}','VariableNames',{'ID','X','Z'});
jointtopordinarysegment{ijk} = jointordinarysegment{ijk}(ismember(jointordinarysegment{ijk}.Z,Zcolumn),:);
jointbottomordinarysegment{ijk} = jointordinarysegment{ijk}(ismember(jointordinarysegment{ijk}.Z,Zbotbeam),:);
jointspecialsegment{ijk} = table((max(jointbottomordinarysegment{ijk}.ID)+1:numel(Xss{ijk,:})+max(jointbottomordinarysegment{ijk}.ID))',Xss{ijk,:}',Zss{ijk,:}','VariableNames',{'ID','X','Z'});
jointtopspecialsegment{ijk} = jointspecialsegment{ijk}(ismember(jointspecialsegment{ijk}.Z,Zcolumn),:);
jointbottomspecialsegment{ijk} = jointspecialsegment{ijk}(ismember(jointspecialsegment{ijk}.Z,Zbotbeam),:);
tablecheck = table((1:numel(anglespecialsegment))',totalspan,anglespecialsegment','VariableNames',{'VariantID','Total Span','Angle'});
VariantIDPassed = tablecheck.VariantID(tablecheck.Angle>=30 & tablecheck.Angle<=60);
jointbase = table((1:numel(jointbase))',jointbase','VariableNames',{'VariantID','Joint Base'});
jointbase = jointbase(ismember(jointbase.VariantID,VariantIDPassed),:);
jointcolumn = table((1:numel(jointcolumn))',jointcolumn','VariableNames',{'VariantID','Joint Column'});
jointcolumn = jointcolumn(ismember(jointcolumn.VariantID,VariantIDPassed),:);
jointtopordinarysegment = table((1:numel(jointtopordinarysegment))',jointtopordinarysegment','VariableNames',{'VariantID','Joint Top Ordinary Segment'});
jointtopordinarysegment = jointtopordinarysegment(ismember(jointtopordinarysegment.VariantID,VariantIDPassed),:);
jointbottomordinarysegment = table((1:numel(jointbottomordinarysegment))',jointbottomordinarysegment','VariableNames',{'VariantID','Joint Bottom Ordinary Segment'});
jointbottomordinarysegment = jointbottomordinarysegment(ismember(jointbottomordinarysegment.VariantID,VariantIDPassed),:);
jointtopspecialsegment = table((1:numel(jointtopspecialsegment))',jointtopspecialsegment','VariableNames',{'VariantID','Joint Top Special Segment'});
jointtopspecialsegment = jointtopspecialsegment(ismember(jointtopspecialsegment.VariantID,VariantIDPassed),:);
jointbottomspecialsegment = table((1:numel(jointbottomspecialsegment))',jointbottomspecialsegment','VariableNames',{'VariantID','Joint Bottom Special Segment'});
jointbottomspecialsegment = jointbottomspecialsegment(ismember(jointbottomspecialsegment.VariantID,VariantIDPassed),:);
Zbottombeam{ijk} = table((1:numel(Zbottombeam{ijk,:}))',Zbottombeam{ijk,:}','VariableNames',{'VariantID','Z Bottom Beam'});
Zbottombeam = Zbottombeam(VariantIDPassed);
for i = 1:height(VariantIDPassed)
jointcolumnnew{i,:} = jointcolumn.("Joint Column"){i}(ismember(jointcolumn.("Joint Column"){i}.Z,jointtopordinarysegment.("Joint Top Ordinary Segment"){i}.Z),:);
jointcolumnnew{i,:} = vertcat(jointbase.("Joint Base"){i},jointcolumnnew{i});
for j = 1:height(jointcolumnnew{i,1})-2
jointIcolumn(j) = jointcolumnnew{i,1}.ID(j);
jointJcolumn(j) = jointcolumnnew{i,1}.ID(j+2);
framecolumn{i,:} = table((1:j)',jointIcolumn',jointJcolumn','VariableNames',{'Frame ID','JointI','JoinJ'});
for i = 1:height(VariantIDPassed)
endTOS{i,:} = jointcolumn.("Joint Column"){i}(ismember(jointcolumn.("Joint Column"){i}.Z,jointtopordinarysegment.("Joint Top Ordinary Segment"){i}.Z),:);
jointTOS{i,:} = vertcat(jointtopordinarysegment.("Joint Top Ordinary Segment"){i},endTOS{i});
jointTOSnew1{i,:} = sortrows(jointTOS{i}(ismember(jointTOS{i}.Z,H),:),2);
jointTOSnew2{i,:} = sortrows(jointTOS{i}(ismember(jointTOS{i}.Z,H*Ns),:),2);
jointTOSnew{i,:} = vertcat(jointTOSnew1{i},jointTOSnew2{i});
for i = 1:height(VariantIDPassed)
for j = 1:height(jointTOSnew1{i,1})-1
jointItos(j) = jointTOSnew1{i,1}.ID(j);
jointJtos(j) = jointTOSnew1{i,1}.ID(j+1);
lastframenumber{i,:} = max(framecolumn{i,1}.("Frame ID"));
frameTOS1{i,:} = table((lastframenumber{i}+1:lastframenumber{i}+j)',jointItos',jointJtos','VariableNames',{'Frame ID','JointI','JoinJ'});
for i = 1:height(VariantIDPassed)
for j = 1:height(jointTOSnew2{i,1})-1
jointItos(j) = jointTOSnew2{i,1}.ID(j);
jointJtos(j) = jointTOSnew2{i,1}.ID(j+1);
lastframenumber{i,:} = max(frameTOS1{i,1}.("Frame ID"));
frameTOS2{i,:} = table((lastframenumber{i}+1:lastframenumber{i}+j)',jointItos',jointJtos','VariableNames',{'Frame ID','JointI','JoinJ'});