Vectorize nested for loops
이전 댓글 표시
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!
답변 (1개)
KSSV
2021년 10월 6일
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.
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!