How to vectorize for loops?
古いコメントを表示
Hi Everybody, I have three for loops and their processing is very slow, I need to speed up the process. For that purpose we need to convert it to vectors . Any help will be strongly encouraged. Below is the code:
for k = 1:size_glcm_3
glcm_sum(k) = sum(sum(glcm(:,:,k)));
glcm(:,:,k) = glcm(:,:,k)./glcm_sum(k); % Normalize each glcm
glcm_mean(k) = mean2(glcm(:,:,k)); % compute mean after norm
glcm_var(k) = (std2(glcm(:,:,k)))^2;
for i = 1:size_glcm_1
for j = 1:size_glcm_2
p_x(i,k) = p_x(i,k) + glcm(i,j,k);
p_y(i,k) = p_y(i,k) + glcm(j,i,k); % taking i for j and j for i
p_xplusy((i+j)-1,k) = p_xplusy((i+j)-1,k) + glcm(i,j,k);
p_xminusy((abs(i-j))+1,k) = p_xminusy((abs(i-j))+1,k) +...
glcm(i,j,k);
end
end
end
All arrays are pre-allocated, size of size_glcm_1 and size_glcm_2 is 512 and size of size_glcm_3 is 1 .
3 件のコメント
Please edit the question and insert more information. Do not post code lines, which are commented, because this confused the readers. Provide some example data, because the typical dimensions matter, e.g. if size_glcm_1 is 10 and size_glcm_2 is 1e7 or the other way around.
Please run the üprofile to find the bottleneck of the code. If 80% of the processing time are spent inside mean2 optimizing the loops will not help that much.
Are the arrays pre-allocated? Is gclm required as result or is it a temporary variable only? p_y is the tranposed p_x, so do you really need to calculate it?
azizullah khan
2015 年 12 月 12 日
編集済み: azizullah khan
2015 年 12 月 12 日
Image Analyst
2015 年 12 月 13 日
Why not use var() instead of std2() and squaring?
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および 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!