how to vectorize this for loops?

4 件のコメント

Stephen23
Stephen23 2018 年 1 月 10 日
@ Siddhesh Karbhari: please delete the screen shot and give us proper text code. A screen shot is useless to us: we cannot edit it, we cannot run it, we cannot copy-and-paste it into the editor, we cannot do anything with it at all. Please give us real code.
Adam
Adam 2018 年 1 月 10 日
We could type it all out into our editor character by character, hoping we don't make a mistake and that we don't have more useful things to do with our lives :)
Siddhesh Karbhari
Siddhesh Karbhari 2018 年 1 月 10 日
編集済み: per isakson 2018 年 1 月 13 日
clc,
octave=3;
lavel=3;
sigma0=sqrt(2);
for i=1:octave
temp_D=D{i};
for j=1:level
scale=sigma0*sqrt(2)^(1/level)^((i-1)*level+j)
p=(level)*(i-1);
figure(1);
subplot(octave,level,p+j);
f=fspecial('gaussian',[1,floor(6*scale)],scale);
L1=temp_img;
if(i==1&&j==1)
L2=conv2(temp_img,f,'same');
L2=conv2(L2,f','same');
temp_D(:,:,j)=L2-L1;
imshow(uint8(255 * mat2gray(temp_D(:,:,j))));
L1=L2;
else
L2=conv2(temp_img,f,'same');
L2=conv2(L2,f','same');
temp_D(:,:,j)=L2-L1;
L1=L2;
if(j==level)
temp_img=L1(2:end-1,2:end-1);
end
imshow(uint8(255 * mat2gray(temp_D(:,:,j))));
end
end
D{i}=temp_D;
temp_img=temp_img(1:2:end,1:2:end);
temp_img=padarray(temp_img,[1,1],'both','replicate');
end
Jan
Jan 2018 年 1 月 10 日
By the way:
% Simpler:
% sigma0 * sqrt(2) ^ (1/level) ^ ((i-1)*level+j)
sigma0 * sqrt(2) ^ (i - 1 + j / level)
With a proper indentation the code looks cleaner: Ctrl-A Ctrl-I

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

回答 (1 件)

Jan
Jan 2018 年 1 月 10 日

1 投票

Neither the inner nor the outer loop can be vectorized. The two imshow commands are impossible to vectorize. Do you really need them? Do you want a vectorization to increase the speed? Then start with using the profiler to find the most time consuming part of the code.

3 件のコメント

Siddhesh Karbhari
Siddhesh Karbhari 2018 年 1 月 10 日
I want to implement this in GPU using MATLAB. What should I do? Can you please guide me?
Jan
Jan 2018 年 1 月 10 日
What do you want to implement on GPU exactly? imshow, subplot, fspecial are not suitable for this, but conv2 will be fine.
Siddhesh Karbhari
Siddhesh Karbhari 2018 年 1 月 10 日
編集済み: Siddhesh Karbhari 2018 年 1 月 10 日
I want to implement SIFT descriptor on GPU.

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

質問済み:

2018 年 1 月 10 日

編集済み:

2018 年 1 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by