vectorization of for loop

5 次查看(过去 30 天)
chabani mohammed ali
编辑: Aashray 2025-2-27
i have this loop, i want to vectorize it if it's possible and i want to know in this case if the vectorization is going to speed the programme or it only depends on function speed.
I=imread('10.bmp');
figure;
imshow(I);
n = input('threshold= ') ;
sizeI = size(I);
C = mat2tiles(I, ceil(sizeI(1:2)./[30 30]));
[a,b]=size(C);
for i=1:a*b
G=reg_growRVB(C{i},n);
C{i}=G;
end
  2 个评论
Sulaymon Eshkabilov
Sulaymon Eshkabilov 2020-11-16
what are your functions reg_growRVB() and mat2tiles()?
chabani mohammed ali
mat2tiles() splits an image into 30*30 images
reg_growRVB(i,n) does an image segmentation for image i
in the for loop the tiles of the originale image are used as argument for the function

请先登录,再进行评论。

回答(1 个)

Aashray
Aashray 2025-2-27
编辑:Aashray 2025-2-27
Hello Chabani,
I am glad you came up with this question, as this unfolds a lot about optimizations which can be done in MATLAB.
% Defining Parameters
nImages = 1500; % Number of images (tiles)
imageSize = [30, 30]; % Size of each image (each tile)
randomImages = cell(1, nImages);
for i = 1:nImages
randomImages{i} = rand(imageSize); % Using random matrices as placeholder for images (tiles)
end
% We can also use mat2tiles() for creating tiles from an image and storing
% them in randomImages cell array. Here, I am creating random images for
% illustration purposes.
threshold = 0.5;
Let me explain the two methods for approaching this problem, and I’ll be more than happy to include a bonus approach in the end:
1. Normal Method (Using a “for” loop):
tic
C_normal = randomImages;
for i = 1:nImages
C_normal{i} = reg_growRVB(C_normal{i}, threshold);
end
time_normal = toc;
In this, we are processing each image one at a time in a sequential manner using a regular for loop. This results in no parallelization or vectorization. So, it’s bound to be slow.
2. Vectorized Method:
tic
C_vectorized = cellfun(@(img) reg_growRVB(img, threshold), randomImages, 'UniformOutput', false);
time_vectorized = toc;
In the vectorized method, we're using “cellfun()” to apply the “reg_growRVB()” function to each image in the “randomImages” array. MATLAB uses SIMD (single instruction, multiple data) optimizations at the hardware level, and thus instead of fetching each image (a tile here) from the memory, it does so in groups thus reducing the memory access time per tile and hereby reducing the overall time for segmentation.
However, the improvement isn't very significant because the “reg_growRVB()” function contains conditional branching, loops, and other operations that can't be fully vectorized.
3. Parallel Method (Bonus method):
tic
C_parallel = randomImages;
parfor i = 1:nImages
C_parallel{i} = reg_growRVB(C_parallel{i}, threshold);
end
time_parallel = toc ;
In this method, we're using “parfor()”, which allows us to take advantage of multiple CPU cores. Each image is processed independently, so the segmentation task is divided across different processors. This significantly reduces the total time required for execution by a factor roughly proportional to the number of processor cores used.
The comparison b/w time taken by each method can be made by executing this script:
fprintf('Time for normal method: %.4f seconds\n', time_normal);
fprintf('Time for vectorized method: %.4f seconds\n', time_vectorized);
fprintf('Time for parallel method: %.4f seconds\n', time_parallel);
TL; DR: Vectorization can speed up the code by reducing loop overhead and using more efficient memory access. But, if the function is complex (here “reg_growRVB()” is a complex function), the performance boost might not be as significant.
I hope this clarifies your question.
Note: The first time you execute the parallel method, it will take more time because during the first execution, MATLAB needs to initialize the parallel workers (i.e., separate MATLAB sessions running on different CPU cores). Subsequent executions are much faster compared to normal/vectorization approach.
Also, I am including some links for further reading which might help you:
  1. “mat2tiles()” function: https://www.mathworks.com/matlabcentral/fileexchange/35085-mat2tiles-divide-array-into-equal-sized-sub-arrays
  2. “reg_growRVB()” function: https://www.mathworks.com/matlabcentral/fileexchange/72944-reggrow
  3. “cellfun()” function: https://www.mathworks.com/help/matlab/ref/cellfun.html
  4. Vectorization in MATLAB: https://www.mathworks.com/help/matlab/matlab_prog/vectorization.html
  5. “parfor()” function: http://mathworks.com/help/parallel-computing/parfor.html

类别

Help CenterFile Exchange 中查找有关 Animation 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by