Improve speed of for loop
1 回表示 (過去 30 日間)
古いコメントを表示
Anna van der Stap
2021 年 3 月 22 日
コメント済み: Anna van der Stap
2021 年 3 月 22 日
I have a for loop as follows:
for z = 1:r %run through data from start to end
x = row14 >= (z-1)*tp & row14 <= z*tp; % run through in steps the size of your time bins
s(z) = nnz(x);
end
However, I'm working with really large matrices, r = 56043, and row14 is 1x19609285 double. As a result this particular for loop takes absolutely ages. Is there any way that I can improve the performance to speed up the process a bit?
0 件のコメント
採用された回答
Walter Roberson
2021 年 3 月 22 日
idx = floor(row14 ./ tp) + 1;
counts = accumarray(idx(:), 1);
counts(end+1:r) = 0;
Or
counts = histcounts(row14, (0:r)*tp);
or
counts = histcounts(row14, 'binwidth', tp);
Caution: the binwidth will not be respected if the data implies more than 2^16 bins.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!