Short function taking 20+ minutes to run

6 visualizzazioni (ultimi 30 giorni)
I'm using a Xeon quad-core processor with 64GB of RAM. The program running the function only has 89 data points. It's now been over 20 minutes and MATLAB is still "busy" computing the program. Does my code below reveal any reason why it's taking so long to compute?
function last15MinsOfDay=last15MinsOfDay(time,price)
% last15MinsOfDay takes the average of prices between 3:45 and 4:00.
z=[1; z];
for i = 1:length(z);
while i < length(z)

Risposta accettata

the cyclist
the cyclist il 21 Lug 2015
When your code hits this line:
while i < length(z)
the value of i is 1, and the length of z is whatever it is. The condition will always be true, because i and z do not change. Therefore, you will never break out of that while loop.
Not sure what you intended, but the while loop seems completely unnecessary here.
  3 Commenti
Cedric Wannaz
Cedric Wannaz il 21 Lug 2015
Modificato: Cedric Wannaz il 21 Lug 2015
Also use the debugger to see what is happening in your code. Press F12 in the editor with the cursor on the line where you want to set a break point (or click on the little dash on the left), then press F5 for running your code (it will stop at the break point), and step manually by pressing F10 or F11 for step or "step in" respectively. Note that at any time you can observe the content/state of variables (in the workspace, by typing variable names in the command window, or by passing the cursor over variables in the editor).

Accedi per commentare.

Più risposte (0)


Scopri di più su Loops and Conditional Statements in Help Center e File Exchange


Community Treasure Hunt

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

Start Hunting!

Translated by