Consider the following matrix, where the first column is the chronological index and the second and third columns contain the data.
Data=
1 5 100
2 10 100
3 5 300
4 15 200
5 5 500
6 15 0
7 10 400
8 5 300
9 10 200
10 10 0
11 5 300
12 10 100
13 15 1000
... ... ...
T ... ...
These data may be thought as orders in an auction or exchange for a single good, where at each time point t (column 1) a new order with "price" (column 2) arrives and the total amount of units demanded at this price at this particular time point is in the column 3. So, for instance, consider an auction where buyers submit their bids for a good, then the data above means:
Row 1: at time t=1 the new order arrives, the price of this order is 5 and the total number of units demanded at this price is 100.
Row 2: at time t=2 ... order with price 10 - the total demand at price 10 is 100
Summary: at time t=2 there are 100 units demanded at price 5 and 100 at price 10
Row 3: at time 3 ... order with price 5 which demands additional 200 units, so the total number of units demanded at price 5 is 300
Summary: at time t=3 there are 300 units demanded at price 5 and 100 at price 10
Row 4: t=4 ... order with price 15 for 200 units, the total demand at price 15 is 200
Summary: t=4 there are 300 units demanded at price 5, 100 at 10, and 200 at 15
...
Summary: t=5 there are 500 units demanded at price 5, 100 at 10, and 200 at 15
Row 6: t=6 the price is 15 but there are 0 units in the third column, meaning that the order was canceled, and there is no demand at price 15
Summary: t=6 there are 500 units demanded at price 5 and 100 at 10
I want to allocate the data to the following two Tx3 matrices, where each row represents the "Summary:" line above:
[Price=5][Price = 10][Price = 15]
[Time = 1] 5 NaN NaN
[Time = 2] 5 10 NaN
[Time = 3] 5 10 NaN
[Time = 4] 5 10 15
[Time = 5] 5 10 15
[Time = 6] 5 10 NaN
[Time = 7] 5 10 NaN
[Time = 8] 5 10 NaN
[Time = 9] 5 10 NaN
[Time = 10] 5 NaN NaN
[Time = 11] 5 NaN NaN
[Time = 12] 5 10 NaN
[Time = 13] 5 10 15
... ... ... ...
[Time = T] ... ... ...
[Price=5][Price = 10][Price = 15]
[Time = 1] 100 NaN NaN
[Time = 2] 100 100 NaN
[Time = 3] 300 100 NaN
[Time = 4] 300 100 200
[Time = 5] 500 100 200
[Time = 6] 500 100 NaN
[Time = 7] 500 400 NaN
[Time = 8] 300 400 NaN
[Time = 9] 300 200 NaN
[Time = 10] 300 NaN NaN
[Time = 11] 300 NaN NaN
[Time = 12] 300 100 NaN
[Time = 13] 300 100 1000
... ... ... ...
[Time = T] ... ... ...
Basically, the two matrices above allow me to get the "prices" and "units" for any point of "time". Note, that each "price" may have discontinuities which appear once the "units" are 0 - so "price=15" appears only at t=4 and exists for two periods only: t=4, t=5 (the order is canceled at t=6) further reappearing at t=13.
I proceed as follows:
1.) Sort the Data matrix by column 2 ("prices") and get the index for unique values in column 2:
Data=sortrows(Data, [2 1]);
[~,~, IndexPrice]=unique(Data(:,2));
Data= IndexPrice=
1 5 100 1
3 5 300 1
5 5 500 1
8 5 300 1
11 5 300 1
2 10 100 2
7 10 400 2
9 10 200 2
10 10 0 2
12 10 100 2
4 15 200 3
6 15 0 3
13 15 1000 3
... ... ... ...
T ... ... ...
2.) Allocate values to the two output matrices:
OutputPrice=NaN(size(Data,1), max(IndexPrice)); %Preallocate matrix
for j=1:max(IndexPrice) %Go column-wise
TempData=Data(IndexPrice==j,:); %Submatrix for unique "price"
for i=1:size(TempData,1)
if TempData(i,3)~=0 %Check for discontinuity (0 in col 3)
OutputPrice(TempData(i,1):end,j)=TempData(1,2); %Fill wiht values
else
OutputPrice(TempData(i,1):end,j)=NaN; % If there is 0 fill with NaNs
end
end
end
OutputUnits=NaN(size(Data,1), max(IndexPrice));
for j=1:max(IndexPrice)
TempData=Data(IndexPrice==j,:);
for i=1:size(TempData,1)
if TempData(i,3)~=0
OutputUnits(TempData(i,1):end,j)=TempData(i,3); %The "units" change in contrast to the "prices"
else
OutputUnits(TempData(i,1):end,j)=NaN;
end
end
end
The key point, of course, is the performance of the code - it seems to be a "brute force" approach to the problem. I would appreciate any suggestions about more efficient ways to solve it.