我假设你是文件中的reading your data,如下所示:
data = xlsread('your_file.xls');
这会为您提供一个包含数据的数字矩阵。然后,您可以通过使用unique 解析第一列和最后一列来重新组织它,然后将结果用作accumarray 的索引以收集中心列中的数据。然后你只需添加行和列标签:
[rowVals, ~, rowIndex] = unique(data(:, 3));
[colVals, ~, colIndex] = unique(data(:, 1).');
A = accumarray([rowIndex colIndex], data(:, 2));
A = [NaN colVals; rowVals A];
结果,对于您上面的示例数据:
A =
NaN 1 2 3
20160101 100 80 200
20170101 150 90 200
如果您有重复的条目(即具有相同日期和标识符的条目),以上将默认将它们相加。如果您希望它做其他事情,您可以提供 function handle 到 accumarray。例如:
A = accumarray([rowIndex colIndex], data(:, 2), [], @mean); % Averages them
A = accumarray([rowIndex colIndex], data(:, 2), [], @(x) x(1)); % Keeps the first entry