1

sourceData如果索引位置位于triggerIndices按顺序列出索引的第二列矩阵(

这可以通过for-loop 轻松完成,但可以通过矢量化方式完成吗?

%// Generation of example data follows
sourceData = randi(1e3,100,1);

%// sourceData = 1:1:1000; %// Would show more clearly what is happening
triggerIndices = randperm(length(sourceData),15);
triggerIndices = sort(triggerIndices);

%// End of example data generation

%// Code to be vectorized follows

increment = 75;
addOn = 100;
for index = 1:1:length(triggerIndices)-1
    sourceData(triggerIndices(index):1:triggerIndices(index+1)-1) = ...
        sourceData(triggerIndices(index):1:triggerIndices(index+1)-1) + addOn;
    addOn = addOn + increment;
end

sourceData(triggerIndices(end):1:end) = ....
    sourceData(triggerIndices(end):1:end) + addOn;
%// End of code to be vectorized
4

1 回答 1

1

如何将所有内容替换为:

vals = sparse(triggerIndices, 1, increment, numel(sourceData), 1);
vals(triggerIndices(1)) = addOn;
sourceData(:) = sourceData(:) + cumsum(vals);

这基本上是此处显示的游程解码的变体。

于 2013-09-09T15:59:19.883 回答