【发布时间】:2014-07-02 23:40:12
【问题描述】:
有没有办法在 MATLAB 中对以下循环进行矢量化?
for j = 1:length(cursor_bin)
cursor_bin(j) = mean(cursor(bin == j));
end
cursor_bin、cursor 和 bin 都是向量。
【问题讨论】:
标签: performance matlab for-loop vector vectorization
有没有办法在 MATLAB 中对以下循环进行矢量化?
for j = 1:length(cursor_bin)
cursor_bin(j) = mean(cursor(bin == j));
end
cursor_bin、cursor 和 bin 都是向量。
【问题讨论】:
标签: performance matlab for-loop vector vectorization
bsxfun 非零 cursor 数组的方法 -
t1 = bsxfun(@eq,bin(:),1:numel(cursor_bin))
t2 = bsxfun(@times,t1,cursor(:))
t2(t2==0)=NaN
cursor_bin = nanmean(t2)
【讨论】:
bin(:) 而不是bin 来确保它是一个列? cursor 也一样:cursor(:).'
bsxfunhere的信息
accumarray 就是这样做的:
cursor_bin = accumarray(bin(:), cursor(:), [], @mean);
【讨论】: