在MATLAB中矢量化循环

时间:2014-05-14 18:49:12

标签: performance matlab for-loop vector vectorization

有没有办法在MATLAB中对以下循环进行矢量化?

for j = 1:length(cursor_bin)
    cursor_bin(j) = mean(cursor(bin == j));
end

cursor_bincursorbin都是向量。

2 个答案:

答案 0 :(得分:5)

accumarray就是这样做的:

cursor_bin = accumarray(bin(:), cursor(:), [], @mean);

答案 1 :(得分:4)

非p bsxfun数组的

cursor方法 -

t1 = bsxfun(@eq,bin(:),1:numel(cursor_bin))
t2 = bsxfun(@times,t1,cursor(:))
t2(t2==0)=NaN
cursor_bin = nanmean(t2)