Boolean Assignment Taking Too Long

2 次查看(过去 30 天)
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 个评论
Walter Roberson
Walter Roberson 2015-7-24
Why are you calling nansum() on a scalar that does not appear to ever be nan?
Max Gawryla
Max Gawryla 2015-7-28
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.

请先登录,再进行评论。

回答(1 个)

Image Analyst
Image Analyst 2015-7-24
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 个评论
Max Gawryla
Max Gawryla 2015-7-28
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by