【问题标题】:Organising large datasets in Matlab在 Matlab 中组织大型数据集
【发布时间】:2016-11-06 00:05:16
【问题描述】:

我有一个问题希望你能帮我解决。

我在 Matlab 中导入了一个大型数据集(200000 x 5 单元格),其结构如下:

'Year' 'Country' 'X' 'Y' 'Value'

第 1 列和第 5 列包含数值,而第 2 到 4 列包含字符串。

我想将所有这些信息安排到一个具有以下结构的变量中:

NewVariable{Country_1 : Country_n , Year_1 : Year_n}(Y_1 : Y_n , X_1 : X_n)

我能想到的就是循环遍历整个数据集以查找 CountryYearXY 变量的名称之间的匹配项,这些变量结合了 ifstrcmp 函数,但这似乎是实现我想要做的最无效的方式。

谁能帮帮我?

提前致谢。

【问题讨论】:

  • 先写点能起作用的东西,以后再考虑优化。
  • 感谢 excaza 的提示。关键是可能有一个我不知道的功能可以用来做到这一点。否则,每次我想提取具体值时,我都必须遍历整个数据集,这不是一个真正的选择,例如,如果我有 500 万条记录而不是 200000 条记录。
  • 哦,有!请看一下 categorical()

标签: matlab cell-array organization large-data-volumes matlab-table


【解决方案1】:

如 cmets 中所述,您可以使用分类数组:

% some arbitrary data:
country = repmat('ca',10,1);
country = [country; repmat('cb',10,1)];
country = [country; repmat('cc',10,1)];
T = table(repmat((2001:2005)',6,1),cellstr(country),...
    cellstr(repmat(['x1'; 'x2'; 'x3'],10,1)),...
    cellstr(repmat(['y1'; 'y2'; 'y3'],10,1)),...
    randperm(30)','VariableNames',{'Year','Country','X','Y','Value'});
% convert all non-number data to categorical arrays:
T.Country = categorical(T.Country);
T.X = categorical(T.X);
T.Y = categorical(T.Y);
% here is an example for using categorical array:
newVar = T(T.Country=='cb' & T.Year==2004,:);

table 类就是为这样的事情而生的,而且非常方便。只需扩展最后一行T.Country=='cb' & T.Year==2004 中的逻辑语句即可满足您的需求。 告诉我这是否有帮助;)

【讨论】:

  • 感谢您的有用回复,@EBH!将数据作为表格导入并使用分类函数正是我所需要的。 :-)
猜你喜欢
  • 2011-04-18
  • 1970-01-01
  • 2015-02-18
  • 1970-01-01
  • 1970-01-01
  • 2013-03-04
  • 2011-06-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多