Logic indexing optimization of computational time
조회 수: 2 (최근 30일)
이전 댓글 표시
I am running a computationally expensive code, and according to MATLAB's profiler, this is the line of code that is taking the most time to run:
b = a(idx,:);
where a is 30000x3 matrix, and idx is a logical 30000x1 array, computed as follows:
idx = a(:,3) >= M + N;
where M and N are integers.
That line of code is run inside a for loop with many other lines with functions such as find. I need to understand why that line of code is taking longer to run than all other lines, and how I could optimize it. Sorry for the low amount of information on the code, for property reasons I cannot divulge much more.
Thank you in advance!
댓글 수: 5
Torsten
2023년 8월 29일
Often, it is the interaction of many factors that makes a code slow. So concentrating on a single command is not the correct approach in my opinion.
That having said, b = a(idx,:) is the optimal way to extract the rows of the matrix "a" where the element of the 3rd column is >= M+N. Nothing to optimize or critisize here.
답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!