在 MATLAB 中格式化来自 excel sheet(两个标准)的数据

Formatting data from an excel sheet (two criteria) in MATLAB

我有一个与最近 相关的问题。我将数据下载为 csv.file。然后我在 MATLAB 中对其进行格式化。我得到格式化的数据,例如:

我想格式化数据以便获得:

换句话说,日期应该在第一列,而两个标识符应该分别在前两行。

我尝试了@gnovice 提供的代码,但我无法调整第二个标识符。代码是:

A = accumarray([rowIndex colIndex], data(:, 4), [], @(x) x(1));
A = [NaN colVals; rowVals A];

其中data等于图片(1).

因此,我获得了一个矩阵 A,例如:

 A = 
      NaN     1    2     3;
 20160101   100   80    90;
 20170101   150   90   200;

我如何调整我的代码以处理第二个标识符并且 A 变为:

 A = 
      NaN     1    2     3;
      NaN    10   10    15;
 20160101   100   80    90;
 20170101   150   90   200;

您可以使用此代码

C = unique([identifier_1,identifier_2],'rows')

并相应地格式化您的数据

points in the right direction. You'll want to collect the columns containing your identifiers (i.e. data(:, [1 3])), pass that to unique with the 'rows' option to find the unique row combinations, then capture the third output to use as the index for aggregation by accumarray。您的最终矩阵格式将只需要更改以说明第二个标识符:

[rowVals, ~, rowIndex] = unique(data(:, 2));
[colVals, ~, colIndex] = unique(data(:, [1 3]), 'rows');
A = accumarray([rowIndex colIndex], data(:, 4), [], @(x) x(1));
A = [NaN colVals(:, 1).'; NaN colVals(:, 2).'; rowVals A];

您的示例数据的结果:

A =

         NaN           1           2           3
         NaN          10          10          15
    20160101         100          80         200
    20170101         150          90         200