Can someone please help me implementing this code without loop ?

1 次查看(过去 30 天)
Hello,
Can someone help me to speed up this code? Without loops
img = imread("my_img.png");
for i=2:256
for j=2:256
v1(i,j) = max(abs(img(i+1,j)-img(i,j)),abs(img(i-1,j)-img(i,j)));
v2(i,j) = max(abs(img(i,j-1)-img(i,j)),abs(img(i,j+1)-img(i,j)));
v3(i,j) = max(abs(img(i-1,j+1)-img(i,j)),abs(img(i+1,j-1)-img(i,j)));
v4(i,j) = max(abs(img(i+1,j+1)-img(i,j)),abs(img(i-1,j-1)-img(i,j)));
end
end
min_v1 = min(v1);
min_v2 = min(v2);
min_v3 = min(v3);
min_v4 = min(v4);
Thanks
  10 个评论
Bruno Luong
Bruno Luong 2018-11-4
编辑:Bruno Luong 2018-11-4
Preallocation or not it's still give outcome vectors containing only 0s.
At this point I'm not even sure the formula inside the for-loop is really what you want, since there are still errors somewhere else after 2/3 corrections.
I suggest you debug your code and post the version that works as you expected, then ask the question because we can't help you to remove a loop if we don't know what is the expected result.

请先登录,再进行评论。

采纳的回答

Bruno Luong
Bruno Luong 2018-11-4
f = rand(4,5);
[m,n] = size(f);
x = 2:m-1;
y = 2:n-1;
v1 = max(abs(f(x+1,y)-f(x,y)),abs(f(x-1,y)-f(x,y)));
v2 = max(abs(f(x,y-1)-f(x,y)),abs(f(x,y+1)-f(x,y)));
v3 = max(abs(f(x-1,y+1)-f(x,y)),abs(f(x+1,y-1)-f(x,y)));
v4 = max(abs(f(x+1,y+1)-f(x,y)),abs(f(x-1,y-1)-f(x,y)));
vmin = min(cat(3,v1,v2,v3,v4),[],3);
alpha = 0.1;
[x,y] = find(vmin>alpha);
x = x+1;
y = y+1;
I_f = [x y]

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by