How to reduce computation time in a code with multiple nested loops and if statements

4 views (last 30 days)
Dear all Community members,
I have written a code that involves multiple nested loops and several if statements which require many iterations. The matrices involved are big that does the computations time consuming. I want to reduce the computation time, either by vectorization or "rewriting" the nested loops.
I have attached an excerption of the code by simplyfing the mathematical operations.
I appreciate all help and tips.
Thanks in advance!

Answers (1)

Srivardhan Gadila
Srivardhan Gadila on 15 Jul 2020
Vectorized code often runs much faster than the corresponding code containing loops (Vectorization).
In addition to that I would suggest you to refer to parfor, Parallel for-Loops (parfor) & Parallel Computing Toolbox.
If you have a gpu then you can make use of GPU Computing.
Note that there would be some additional time consumed to start parallel pool of workers in case of parfor. In the case of GPU computing some additional time would be consumed when data is transferred to GPU from MATLAB & vice-versa.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!