Vectorizing the loop

This code is similar to the histc function, but each element has weights associated with it. I'm looking to either use an alternate function or to vectorize it to make things faster
I have arrays A and B, and w is an array of weights associated with each element in A.
I'm hoping to calculate:
for i=1:length(B)
x(i) = sum((A>B(i)).*w)
end
Any help is appreciated. Thanks.

5 件のコメント

Junaid
Junaid 2012 年 4 月 24 日
I think it can be done, could you show small examples for A, B, w.
Nirmal
Nirmal 2012 年 4 月 24 日
The arrays are fairly long, but to give a really simple example
A = [ 1 3 2 5 3 2 5 6], B = [1 2 3 4 5], w = [0.1 0.03 0.1 0.1 0.05 0.2 0.1 0.01]
Walter Roberson
Walter Roberson 2012 年 4 月 24 日
cumsum(histc(...)) .* w ??
Nirmal
Nirmal 2012 年 4 月 24 日
Not sure if that works - particularly because cumsum(histc(...)) will have the size of B and w will have the size of A.
Sean de Wolski
Sean de Wolski 2012 年 4 月 24 日
+1

サインインしてコメントする。

 採用された回答

Sean de Wolski
Sean de Wolski 2012 年 4 月 24 日

0 投票

x = w*bsxfun(@gt,A.',B)

1 件のコメント

Nirmal
Nirmal 2012 年 4 月 24 日
Awesome

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by