Formatting data from an excel sheet in MATLAB

后端 未结 1 1273
情书的邮戳
情书的邮戳 2021-01-23 01:46

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

1条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-23 02:30

    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
    

    0 讨论(0)
提交回复
热议问题