Filter löschen
Filter löschen

How do I make my code faster. It now takes 2 hrs

2 Ansichten (letzte 30 Tage)
Chirag Anand
Chirag Anand am 29 Mär. 2018
Kommentiert: Chirag Anand am 5 Apr. 2018
Hi, I have a code in which the I have to find the pressure at each point. Hence at each point of the square/rectangular grid has to be found out. this requires the element wise multiplication of an array of scalar values with a 2x2 matrix. Because of this requirement i need to use nested loops and hence it takes a lot of time to compute. Is there an easire way?
  6 Kommentare
Jos (10584)
Jos (10584) am 1 Apr. 2018
The most important step: comment the code extensively!
(you will thank me for this a year from now)
Adam
Adam am 3 Apr. 2018
doc profile
is the first place to start. Never start trying to optimise code without knowing exactly which parts of the code are actually slow and what the maximum effect you could get from optimising a given part is!

Melden Sie sich an, um zu kommentieren.

Antworten (1)

William Smith
William Smith am 3 Apr. 2018
1) Not clear from your code, but the first step is always to vectorize. Matlab is good at matrix operations (it's not called Mat lab for nothing). You appear to have loops looping through each element of your matrices.
2) Once you've done that, try things like parfor() in the parallel computation toolbox. But only once you've thoroughly vectorized your code.
  1 Kommentar
Chirag Anand
Chirag Anand am 5 Apr. 2018
Hi, I have tried vectorizing and it does cut down a lot. but i have had to change it because I now need to find a matrix for each value in the grid. So each value has to be multplied with the matrix and the pressure at each point needs to be calculated

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by