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!