Vectorization
If you look at all the calls to mean
, you see that what you are calculating in each loop is a "Cumulative moving average", but in the oposite direction of what one usually would do.
A cumulative moving average can be calculated quite simply using the cumulative sum, cumsum
, and dividing by the number of elements. Since you're starting with all elements and up with only one, we need to flip it.
The running mean, (or cumulative moving average) can be calculated like this:
- First flip
dens
upside down - Take the cumulative sum of it
- Divide each element by the number of elements counted
- Flip it back up
Thus:
running_mean = flipud(cumsum(flipud(dens))./(1:numel(dens)).');
Now, we need to find which element is larger, the cumulative sum, or the original density variable. You are not including the first element of the cumulative sum, and you are appending dens(end)
at the end of the vector.
The maximum value of each row of a matrix can be found using max(A,[],2)
, where the number two represent the second dimension (columns). So, the values we want to use are:
max([running_mean(2:end), dens(2:end)],[],2)
In addition, we need to append the last element, so:
output = [max([running_mean(2:end), dens(2:end)],[],2); dens(end)];
To summarize, the entire operation requires no loops, one call to cumsum
, and one call to max
. In addition we need to flip it twice, but that takes virtually no time.
In total, your function can be written like this:
function output = my_function(vector) n = numel(vector); % numel, not length output = zeros(n,1) % prior to R2015b, use: output(n,1) = 0 % Create an anonymous function called density: density = @(vector) 1000*(1 - (vector+288.9414)./(508929.2.*(vector+68.12963)).*(vector-3.9863).^2); % Calculate the density: dens = density(vector); % Calculate the running mean, and create the final output vector: running_mean = flipud(cumsum(flipud(dens))./(1:numel(dens)).'); output = [max([running_mean(2:end), dens(2:end)],[],2); dens(end)]; end
Time in Octave shows the improvement, as a function of vector size. For vectors with only ten elements, the execution time is 25% of the original code.

Review of your code:
Don't use input
as a variable name! input
is used to ask users for a command line input. Using it as a variable name makes the function useless, but will also potentially create strange errors or warnings. (I called it vector
in this review. You should pick a more descriptive name)
The same goes with max
, mean
, length
etc. Don't use function names as variable names.
Your input is a vector, so no need to specify that you always want the first column. Use linear indexing instead of subscripts. It's much faster, and you won't mess up the dimensions (oops, it was a column vector, not a row vector).
Don't use size(vector,1)
, and don't use length(vector)
, use numel(vector)
. It's faster and more robust.
You are pre-allocating output
. Very good! That's a common bottleneck.
output = vector*0;
is clever. Prior to Matlab R2015b zeros(n,1)
was actually far from the fastest way to create a vector of zeros! In fact, output(n,1) = 0
was up to 500 times faster! This is because zeros
creates a matrix, then fills the elements with zeros, where as the latter just creates a matrix, where the values are zero by default.
In your loop, don't use i
as the iterator, it is used for the imaginary unit (sqrt(-1)
). It's common to use for instance ii
, jj
etc.
If your density
function is only what you have posted there, then you can simply create an anonymous function inside the main function:
density = @(vector) 1000*(1 - (vector+288.9414)./(508929.2.*(vector+68.12963)).*(vector-3.9863).^2);
Call it like:
dens = density(vector);
Review of reviews:
There are a few things I don't like about the other reviews that I want to point out:
"You don't need to specify that 1
when indexing a vector, MATLAB knows". Many Matlab users use max(A)
, when they want to find the maximum value of all columns in a matrix. It's much simpler than max(A, [], 1)
. You might however come into trouble if A
has varying number of rows (and matrices often do). Because the moment that matrix has only one row, max(A)
will give the maximum value of the entire vector, instead of each (one row) column. This can give you a major headache!
"Why not use length
?" Well, in this case, length
is much better than size
, but numel
should still be the preferred choice. length
is better than size
for two reasons. Using size
limits the function to only work on column vectors only, and will fail on row vectors. Second, numel
is much faster than length
for large vectors, and both are much faster than size
for all vectors.
"Don't use the profiler, use tic/toc
". True, the profiler includes the time it takes to run itself, but it's still the easiest way to identify bottlenecks. For exact timing however, don't use tic/toc
. timeit
was introduced in Matlab R2013b, and is much better than tic/toc
. If you are using an older version, the function is available at the File Exchange. tic/toc
is also dangerous, because it will reset the timer if tic
is called inside another function. Have a look at this question on SO from three years ago.
density
function defined? It isn't defined in the standard MatLab functions\$\endgroup\$density
is an external function I declared. It is not relevant for my question, asdens
is also a10x1
double vector\$\endgroup\$output(i,1)= max(mean(dens(i+1:a,1)),dens(i+1,1));
, thus the vectorization I mentioned\$\endgroup\$density
does matters because you might be able to calculateoutput(i,1)
from values inoutput
rather than taking a mean of values indens
. My offhand thought is that you are iterating in the wrong direction. Given what you are doing, you should iterate down not up and calculate the mean as you go. Someone like syb0rg with more knowledge of Matlab could probably give you better help if you offered a working example with all code.\$\endgroup\$density
function in the last edit\$\endgroup\$