clear
clc
sinvt=6;
einvt=0;
nstage= 4;
nstate= 7;
countr=1;
countc=1;
for t = 1:1:nstage
    T = t+1;
          countc= ((T-1)*nstate)+1;
                for i=0:1:1*(nstate-1)
                    for x= i:-1:0
                        nexts = i-x;
                            if x==0 & nexts-i==0
                            r(countr,countc)=0.001;
                             xx(countr,countc)=x;
                             countc=countc+1;
                            else 
                            if t==1
                            r(countr,countc)=7*x+2;
                             xx(countr,countc)=x;
                            end
                            if t==2
                            r(countr,countc)=3*x+7;
                             xx(countr,countc)=x;
                            end
                            if t==3
                            r(countr,countc)=4*x+5;
                             xx(countr,countc)=x;
                            end
                            countc=countc+1;
                            end
                      end
                    countr=countr+1;
                    countc= ((T-1)*nstate)+1;
                end
                countr=(t*nstate)+1;
            end
            r;
 % one source and one sink.
 
  h=size(r)   
 rownum=nstate+1;
  rr(1,:)=r(sinvt+1,:);
  xxx(1,:)=xx(sinvt+1,:);
  for u=2:1:h(1,1)-2*nstate+1
      rr(u,:)=r(rownum,:);
        xxx(u,:)=xx(rownum,:);
      rownum=rownum+1;
  end
  rr(u+1,:)=r(rownum-1+einvt+1,:);
  xxx(u+1,:)=xx(rownum-1+einvt+1,:);
  colnum=nstate+1;
  rrr(:,1)=rr(:,sinvt+1);
    xxxx(:,1)=xxx(:,sinvt+1);
  for u=2:1:h(1,2)-3*nstate+1
      rrr(:,u)=rr(:,colnum);
      xxxx(:,u)=xxx(:,colnum);
      colnum=colnum+1;
  end
 rrr(:,u+1)=rr(:,colnum-1+einvt+1);
     rrr
      xxxx(:,u+1)=xxx(:,colnum-1+einvt+1);
  xxxx
  
   xlswrite('costinvest.xlsx',rrr)
 xlswrite('decisioninvest.xlsx',xxxx)