Clear Filters
Clear Filters

Logic indexing optimization of computational time

2 views (last 30 days)
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 Comments
Rafael Félix Soriano
Rafael Félix Soriano on 29 Aug 2023
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 on 29 Aug 2023
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.

Sign in to comment.

Answers (0)

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!