mardi 11 juillet 2017

Generate a matrix by using the coordenates of other matrix and by summing - matlab

I have three matrixs :

  1. periods
  2. CFF
  3. dates that is the aggregation of matrix periods into intervals

    . dates(1,1) represents the interval [20170101-20173112[

    . dates(1,2) represents the interval [20180101-20183112[

    . dates(1,3) represents the interval [20190101-20193112[

    and so on

My ideia is to aggregate the values in CFF to have a matrix with values correspondent to matrix dates:

cashflows(1,1)=CFF(1,1)+CFF(1,2)+CFF(1,3)+CFF(1,4) because the correspondent periods are between data(1,1) and data (1,2)

cashflows(1,2)=CFF(1,5)+CFF(1,6)+CFF(1,7)+CFF(1,8) because the correspondente periods are between data(1,2) and data(1,3)

cashflows (1,3)=CFF(1,9) + CFF(1,10) because the correspondente periods are greater or equal than data(1,3)

.. the same ideia for all lines

My code is:

periods=[ 20170206 20170506 20170806 20171106 20180206 20180506 20180806 20181106 20190206 20190506;
    20170402 20180402 0 0 0 0 0 0 0 0;
    20170228 20170831 20180228 0 0 0 0 0 0 0;
    20171016 20181016 20191016 0 0 0 0 0 0 0;
    20171025 0 0 0 0 0 0 0 0 0]; %matrix with dates to aggregate

CFF= [860.250000000000 860.250000000000 860.250000000000 860.250000000000 860.250000000000 860.250000000000 860.250000000000 860.250000000000 860.250000000000 300860.250000000;
    68750.0000000000 1068750 0 0 0 0 0 0 0 0;
    85020 85020 13085020 0 0 0 0 0 0 0;
    229350 229350 5729350 0 0 0 0 0 0 0;
    34194000 0 0 0 0 0 0 0 0 0];

dates=[20170101 20180101 20190101;20170101 20180101 0;20170101 20180101 0;20170101 20180101 20190101;20170101 0 0];

[r2,c2]=size(dates);
[r1,c1]=size(CFF);

for i=1:r2
for j=1:c2
    cashflows(i,j)=0;
for jj=1:c1
    if dates(i,j)==0
       cashflows(i,j)=0;
    elseif dates(i,j)<= periods(i,jj)<dates(i,j+1)
        cashflows(i,j)=cashflows(i,j)+CFF(i,jj);
    elseif periods(i,jj)>=dates(i,j)
         cashflows(i,j)=CFF(ii,jj);
    end
end
end
end

The result should be these:

cashflows= [3441.0 3441.0 301720.5; 68750.0 1068750.0 0.0; 40341059.0 13085020.0 0.0; 229350.0 229350.0 5729350.0; 34194000.0 0.0 0.0]

And I'm not even close, could u help please?

Aucun commentaire:

Enregistrer un commentaire