由于与 Matlab 的内置函数相比,M 级操作的开销,在用户 M 代码中对复杂数据实施排序可能会在性能方面有所损失。尝试根据 Matlab 现有的矢量化函数重新构建操作。
根据您的评论,听起来您正在对单元格结构内的单值键进行排序。通过将排序键提取到原始数字数组并在其上调用内置函数,您可能可以获得很好的加速sort
。
%// An example cell array of structs that I think looks like your input
c = num2cell(struct('foo',{'a','b','c','d'}, 'bar',{6 1 3 2}))
%// Let's say the "bar" field is what you want to sort on.
key = cellfun(@(s)s.bar, c) %// Extract the sort key using cellfun
[sortedKey,ix] = sort(key) %// Sort on just the key using fast numeric sort() builtin
sortedC = c(ix); %// ix is a reordering index in to c; apply the sort using a single indexing operation
reordering = cellfun(@(s)s.foo, sortedC) %// for human readability of results
如果要对多个字段值进行排序,请从 n 个单元格中提取所有 m 个键值到一个 n×m 数组中,列按优先级降序排列,然后sortrows
在其上使用。
%// Multi-key sort
keyCols = {'bar','baz'};
key = NaN(numel(c), numel(keyCols));
for i = 1:numel(keyCols)
keyCol = keyCols{i};
key(:,i) = cellfun(@(s)s.(keyCol), c);
end
[sortedKey,ix] = sortrows(key);
sortedC = c(ix);
reordering = cellfun(@(s)s.foo, sortedC)
Matlab 中性能的关键之一是将数据放在原始数组中,并对这些原始数组使用矢量化操作。Matlab 代码看起来像 C++ STL 代码,带有算法和对比较函数等的引用,通常会很慢;即使您的代码在 O(n) 复杂度方面很好,用户级 M 代码操作的固定成本,尤其是在非原始代码上,也可能是一个杀手。
Also, if your structs are homogeneous (that is, they all have the same set of fields), you can store them directly in a struct array instead of a cell array of structs, and it will be more compact. If you can do more extensive redesign, rearranging your data structures to be "planar-organized" - where you have a struct of arrays, reading across the ith elemnt of all the fields as a record, instead of an array of structs of scalar fields - could be a good efficiency win. Either of these reorganizations would make constructing the sort key array cheaper.