is there any way that can speedup the process of following code... A and B are 50000x20 matrices..?

3 views (last 30 days)
indx=1;
for f=1:length(A)
for b=1:length(B)
if (A(f,1:6)==B(b,1:6))&(A(f,7)==B(b,7))
AA(indx,:)=A(f,[1:7 11]);
BB(indx,:)=B(b,1:11);
else
end
end
indx=indx+1;
end

Accepted Answer

Jos (10584)
Jos (10584) on 20 Feb 2018
Your looking for corresponding rows in A and B. intersect might help you
[~,ia,ib] = intersect(A(:,1:7), B(:,1:7), 'rows') ;
AA = A(ia,:) ;
BB = B(ib,:) ;
btw, you loop using length. But length only returns the longest of the dimensions. If you want to loop over rows, use size with the dimension argument, as in size(..., 1)
  3 Comments
Lakshmi Chodavarapu
Lakshmi Chodavarapu on 20 Feb 2018
Thanks a lot for valuable suggestions and inputs Guillaume. The code is running really well with intercept Jos. Thanks a lot!

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements 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!