有没有办法在MATLAB中对以下循环进行矢量化?
for j = 1:length(cursor_bin)
cursor_bin(j) = mean(cursor(bin == j));
end
cursor_bin
,cursor
和bin
都是向量。
答案 0 :(得分:5)
accumarray
就是这样做的:
cursor_bin = accumarray(bin(:), cursor(:), [], @mean);
答案 1 :(得分:4)
bsxfun
数组的 cursor
方法 -
t1 = bsxfun(@eq,bin(:),1:numel(cursor_bin))
t2 = bsxfun(@times,t1,cursor(:))
t2(t2==0)=NaN
cursor_bin = nanmean(t2)