我喜欢使用cellfun对于绘制操作,而不是循环,例如,如果我拥有多套传感器数据,每个组都有多个列(因为每套多个传感器),它使用
numOfSensors = 5;
numOfSets = 6;
%% sample data preparation
x = 1:100;
y = rand(length(x), numOfSets*numOfSensors);
yCell = mat2cell(y, 100, numOfSensors*ones(1,numOfSets)); % this is my sensor data
scaleCell = num2cell(fliplr(cumsum(1:numOfSets)));
yCell = cellfun(@(x, scale)x.*scale, yCell, scaleCell, 'unif', false);
%% plot preparation
nameCell = arrayfun(@(x)['sensor set ' num2str(x)], 1:numOfSets, 'unif', false);
colorCell = num2cell(lines(numOfSets), 2)';
%% plot
figure, hold all,
set(gca, 'ColorOrder', [0 0 0], 'LineStyleOrder', {'-','--','-*','-.',':'})
h = cellfun(@(y, name, c)plot(x, y, 'linewidth', 1.5, 'displayName', name, 'color', c), yCell, nameCell, colorCell, 'unif', false);
hh = cellfun(@(x)x(1), h, 'unif', false);
legend([hh{:}])
是非常方便而不是循环。此示例绘制所有数据集,每个数据集都以其自己的颜色绘制,每个数据集使用其他线型绘制。图例只显示每个数据集(注意:这也可以通过使用hggroups来完成)。
或者更简单的使用情况 - 我再次有数据的单元阵列,并希望有在一个简短的看法:
figure, hold all, cellfun(@plot,dataCell)
就是这样,一条线,速度非常快,在命令行。另一个很好的用例是使用mean(),max(),min(),std()等压缩高维数据数值数据,但你已经提到了这一点。如果数据的大小不统一,这变得更加重要。
相关问题: http://stackoverflow.com/questions/16143314/matlab-arrayfun-cellfun-spfun-and-structfun-vs-simple-for-loop – Shai