Boolean Assignment Taking Too Long

2 views (last 30 days)
Max Gawryla
Max Gawryla on 24 Jul 2015
Commented: Max Gawryla on 28 Jul 2015
I have code that is running exceptionally slowly. I ran it with time on and it's telling me the majority of my time is being spent on a line with a simple "false" assignment. Similarly, a simple boolean in an if is also taking an absurd amount of time. Other lines with functions take time as well as expected, but these two seemingly simple lines confuse me. Attached is a screenshot the portion of the code in question with my issue being on lines 26, 29, and 30. It doesn't have to do with the number of calls; although the line is called a high number of times, other lines within the same if statement are called an equal amount of time but take next to no time comparatively. Why is it taking so long?
  2 Comments
Walter Roberson
Walter Roberson on 24 Jul 2015
Why are you calling nansum() on a scalar that does not appear to ever be nan?
Max Gawryla
Max Gawryla on 28 Jul 2015
nansum is being used because I have missing data. I need changecount to have the same first dimension as fcstChangesAbs and count how many times in a given row fcstChangesAbs is a real, nonzero number. If fcstChangesAbs(i,:) is all NaN values, I need changecount to reflect that. I preallocated changeCount as nan(length(fcstChangesAbs),1) and simply update it if any value in fcstChangesAbs is non-nan.

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 24 Jul 2015
What is fcstChangesAbs? Is it the absolute value of some array? If so it's gauranteed to be 0 or positive, so why are you using the ==0 test in the else statement? It's not necessary unless you think fcstChangesAbs will be negative for some elements.
Also, preallocate changeCount:
changeCount = zeros(size(fcstChangesAbs ));
That way is faster and you won't have to set it explicitly to zero, which takes time.
Also, what is i? Is it a constant or is this in a loop over i also? Or is i a vector? If it's in a loop over i, and i is big, you can have i be the inner loop instead of the outer loop - that will make it run faster if fcstChangesAbs is a really big array. But I don't think i is a loop iteration variable or a constant because you do nansum(changeCount(i, 1)) which doesn't make sense if i is just a single number because there's nothing to sum up. However having i as a vector also doesn't make sense in the line where you do the if test because that if would produce a vector and having a vector in an if should usually have an any() or all() wrapped abound the test. So overall, not much in this code makes sense.
And which line is taking the majority of the time? You say it's the simple false assignment (line 24, the first line of code you show), but that doesn't seem to be corroborated by the numbers to the left of the code.
  1 Comment
Max Gawryla
Max Gawryla on 28 Jul 2015
fcstChangesAbs also contains NaN values as well as zeros and positive values. I'm using ==0 to differentiate from where there is no value in the matrix. i is just the loop iterator that goes 1:length(fcstChangesAbs). The line in question is line 30.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!