I would like to speed up my two for loop with accumarray (I have old version of MatLab and I've read on internet that accumarray will speed up my operation)
if true
for i = 1:size(img, 1)
for j = 1:size(img, 2)
R = idx(i, j, 1);
G = idx(i, j, 2);
B = idx(i, j, 3);
H(R,G,B) = H(R,G,B) + 1;
end
end
end

 採用された回答

Stephen23
Stephen23 2018 年 11 月 7 日
編集済み: Stephen23 2018 年 11 月 7 日

1 投票

This gives exactly the same output as your code:
H = accumarray(reshape(idx,[],3),1);
First I ran your code on some random arrays:
img = randi(6,5,4,3);
idx = img;
H = zeros(6,6,6);
for i = 1:size(img, 1)
for j = 1:size(img, 2)
R = idx(i, j, 1);
G = idx(i, j, 2);
B = idx(i, j, 3);
H(R,G,B) = H(R,G,B) + 1;
end
end
Then compared the outputs:
>> Z = accumarray(reshape(idx,[],3),1);
>> isequal(Z,H)
ans = 1

その他の回答 (0 件)

カテゴリ

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

製品

リリース

R2013b

質問済み:

2018 年 11 月 7 日

編集済み:

2018 年 11 月 7 日

Community Treasure Hunt

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

Start Hunting!

Translated by