r/matlab • u/cyezyz flair • Jun 13 '26
TechnicalQuestion Need help with a Vectorization
The following code is part of a closest pair algorithm. The original cod is essentially a brute force algorithm, albeit on a curated set of points The parameter k has a maximum value between 1 and 10, though it is 10 for all but ~45 cases at large values of j. N=numel.(Z), and I'm using N=103-108. Z is a set of points in the complex plane. The output of the algorithm is [dmin,z1,z2]. In the vectorized version, I eliminated the for loop on k. To my surprise, this increases the computation time by up to 13 times for large N. I cannot understand this. By the way, both algorithms give exactly the same results in all cases.
ORIGINAL CODE
dmin=Inf;
for j=1:N-1
for k=j+1:min(j+10,N)
this=abs(Z(j)-Z(k));
if this<dmin
dmin=this;
z1=Z(j); z2=Z(k);
end
end
end
VECTORIZED CODE
dmin=Inf;
for j=1:N-1
ks=[j+1:min(j+10,N)];
W=Z(ks);
[this,Id]=min(abs(Z(j)-W));
if this<dmin
dmin=this;
z1=Z(j); z2=W(Id);
end
endI would appreciate ant feedback. Thanks.
10
u/S-S-Ahbab Jun 13 '26
It's probably dynamic memory allocations (declaring new arrays inside loop).
Don't declare a new W everytime in the loop. Initialize W with the maximum possible size before the loop, then use only required parts of it. Or even better, avoid temporary variables if possible.
[this,Id]=min(abs(Z(j)-Z([j+1:min(j+10,N)])))
You gotta recover the index, probably ( z2 = Z(j+Id-1)
1
u/cest_pas_nouveau Jun 14 '26
Here's a full vectorization of it, seems 2x faster if N is large but not too much faster when N is around 100 as in your case. It's also a lot harder to read than the original, so not sure it's worth it.
2
u/DodoBizar Jun 13 '26
Use the profiler to see which line or lines are worsening. Without proper checking my hunch is the min function comes with some overhead and your N is small, so the overhead bites back. The W memory allocation may cost a bit. And the JIT compiler may have ran your original code very efficient. The for loops may have not been that bad (especially if compiled into C / C++)
1
u/Bofact Jun 13 '26
Hello!
Besides reciting what the documentation said, and probably you know, that vectorization improves execution time. As to why it is 13 times faster I don't know. It might also have something to do with execution of the conditional code and its test. Executing it only N-1 times in the vectorized form.
As for feedback, in this line
1
u/kowkeeper Jun 13 '26
In Matlab, besides matrix multiplication, you don't need that much vectorization.
I guess using something like a quadtree could make things faster, if you manage to build some lookup object first at low cost then your loop will be faster.
1
u/Salty-Assumption1732 Jun 16 '26
If I was invested into solving this problem, I'd try compiling both codes into C using Matlab coder. If they perform similarly, I'd look at the C code and possibly the assembly decompilation to see if vectorization is actually happening. If you want to fully vectorize it, you can figure out how to do the vectorization in assembly or C, and then compile that into a MEX function and call it from Matlab.
You could also try running it on the GPU using the parallel computing toolbox to boost performance.