Hi, I am trying to vectorize following for loop. Need some help??

1 回表示 (過去 30 日間)
Parag Patil
Parag Patil 2016 年 1 月 26 日
コメント済み: Parag Patil 2016 年 1 月 26 日
accumulator=zeros(numThetas,numRhos);
accx_n=zeros(numEdgePixels,numThetas);
for j=1:numThetas
accumulator(j,:)=[0 histcounts(accx_n(:,j),rho)];
end
  2 件のコメント
Walter Roberson
Walter Roberson 2016 年 1 月 26 日
We do not know whether rho is a scalar (acting as a bin count) or a vector (acting as edge information)
Parag Patil
Parag Patil 2016 年 1 月 26 日
Its a row vector. Say, rho=[-1.3415:1:1.3415] Thank you.

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

採用された回答

Guillaume
Guillaume 2016 年 1 月 26 日
This is a case where the 'old' histogram functions work better than the newer one. If you pass a matrix to histc, it returns the histogram of each column, exactly what you're doing right now with your loop.
You have to watch out that histc and histcounts do not behave exactly the same with regards to the edges (assuming rho is an edge vector), so you may have to modify your rho slightly.
accumulator = [zeros(size(accx_n, 1), 1), histc(accx_n, rho)]
  3 件のコメント
Guillaume
Guillaume 2016 年 1 月 26 日
編集済み: Guillaume 2016 年 1 月 26 日
Unfortunately, there's no workaround for histcounts. The best you could do is parallelise the loop with parfor.
edit: saying that you can reproduce your usage of histcounts with discretize and accumarray. As discretize uses the same binning method as histcounts you 'll get exactly the same result:
bins = discretize(accx_n, rho);
rows = repmat(1:size(accx_n, 2), size(accx_n, 1), 1);
accumulator = [zeros(size(accx_n, 2), 1) accumarray([rows(:), bins(:)], 1)]
No guarantee that it is faster than the loop, due to the matrix resizing of rows and bins.
Parag Patil
Parag Patil 2016 年 1 月 26 日
Thanks..histc worked but I will try this one too.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by