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 个评论
Rafael Félix Soriano
Thank you for your answer Dyuman. My point is that having several lines of code running find, none of them are taking long. It is the one that I posted (b = a(idx,:)) that is taking significantly longer than all others. That is the only part of the code giving me problems.
I am sorry but I am not allowed to divulge any more info.
Torsten
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 个)

产品


版本

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by