문제

Is there a way to vectorize the following loop in MATLAB?

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

cursor_bin, cursor, and bin are all vectors.

도움이 되었습니까?

해결책

accumarray does just that:

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

다른 팁

bsxfun approach for non-zero cursor arrays -

t1 = bsxfun(@eq,bin(:),1:numel(cursor_bin))
t2 = bsxfun(@times,t1,cursor(:))
t2(t2==0)=NaN
cursor_bin = nanmean(t2)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top