Hello Haolun, here is the logic..
% Assumming input Data be like this..
inputData =[
11 21062019 1000
11 22062019 1500
11 23062019 2000
11 24062019 2500
22 21062019 3000
22 22062019 3500
22 23062019 4000
22 24062019 4500
33 21062019 5000
33 22062019 5500
33 23062019 6000
33 24062019 6500];
IDs =unique(inputData(:,1));
Dates = unique(inputData(:,2));
totalIDs = length(IDs);
totalDates = length(Dates);
% logic to create output data
for count1 = 1:totalIDs
outputData{1,count1+1} = IDs(count1);
end
for count2 = 1:totalDates
outputData{count2+1,1} = Dates(count2);
end
for count1 = 1:totalIDs
for count2 = 1:totalDates
ID_val = IDs(count1);
Date_val = Dates(count2);
ID_Column_Indices = find(ismember(inputData(:,1),ID_val));
Date_Column_Indices = find(ismember(inputData(:,2),Date_val));
require_price_val = [];
for i=1:length(ID_Column_Indices)
for j=1:length(Date_Column_Indices)
if ID_Column_Indices(i)==Date_Column_Indices(j)
index = ID_Column_Indices(i);
require_price_val = inputData(index,3);
end
end
end
outputData{count2+1,count1+1} = require_price_val;
end
end
inputData
outputData
% Output data will be display as..
outputData =
[] [ 11] [ 22] [ 33]
[21062019] [1000] [3000] [5000]
[22062019] [1500] [3500] [5500]
[23062019] [2000] [4000] [6000]
[24062019] [2500] [4500] [6500]
Please see attached script and run it and see the result.. Thanksss... :)
Best Answer