I import data from an excel file using the command xlsread
. The data look like the following:
I would like to format these data so that the output
I'll assume you are reading your data from the file as follows:
data = xlsread('your_file.xls');
Which gives you a numeric matrix containing your data. You can then reorganize it by parsing your first and last columns using unique, then using the results as indices into accumarray to collect the data in the center column. Then you just add the row and column labels:
[rowVals, ~, rowIndex] = unique(data(:, 3));
[colVals, ~, colIndex] = unique(data(:, 1).');
A = accumarray([rowIndex colIndex], data(:, 2));
A = [NaN colVals; rowVals A];
And the result, for your sample data above:
A =
NaN 1 2 3
20160101 100 80 200
20170101 150 90 200
If you have duplicate entries (i.e. entries that have the same date and identifier), the above will sum them by default. You can provide a function handle to accumarray
if you'd like it to do something else. For example:
A = accumarray([rowIndex colIndex], data(:, 2), [], @mean); % Averages them
A = accumarray([rowIndex colIndex], data(:, 2), [], @(x) x(1)); % Keeps the first entry