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!