Vectorize nested for loops
1 view (last 30 days)
Show older comments
Hi everybody,
I am currently trying to vectorize the following nested for loop:
% Definitions
x = 10;
A = zeros(3,10000);
B = zeros(3,5000);
C = zeros(1,5000);
% .....
% A and B are filled
%.....
% Loop
for i=1:length(A)
for k=1:length(B)
if(A(:,i) - B(:,k) <= x)
C(k) = true;
end
end
end
In the end it is intended to use B matrices with much more elements, e.g. B = 3 x 10e06.
That's why I would like to speed up the process, vectorizing it.
Thanks in advance!
0 Comments
Answers (1)
KSSV
on 6 Oct 2021
You can reshape the matrices and get what you want. But note that, C will be over written when i-index changes.
You may proceed something like shown.
A1 = reshape(A,3,1,10000) ;
C = A1-B<=x ;
Now think of C.
0 Comments
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!