MATLAB: How to increase speed when taking the max from multidimensional array with -inf

infmaxmultidimension arrayspeed

Dear all,
I'm trying to calculate the max from a large multidimensional array where there are a lot of elements with -inf. Because the multidimensional array is large (around 100 million elements), I want matlab to ignore the -inf values when calculating the max. Another problem is the number of -inf can be different in each dimension, so I can't just trim the multidimensional array into a smaller one. Right now I think when I use max, the function is incorporating -inf in the calculation, which makes the computation slower.
To illustrate:
M=-inf*ones(1,3,1000000);
M(1,1,1:2)=[1 .5];
M(1,2,1:2)=[2 1.5];
M(1,3,1:2)=[2.3 3];
tic
max(M,[],3)
toc
N=ones(1,3,2);
N(1,1,1:2)=[1 .5];
N(1,2,1:2)=[2 1.5];
N(1,3,1:2)=[2.3 3];
tic
max(N,[],3)
toc
ans =
1 2 3
Elapsed time is 0.009900 seconds.
ans =
1 2 3
Elapsed time is 0.004949 seconds.
My matlab version is R2013a. Thank you again for all your time!

Best Answer

Locating and removing the -inf is going to be slower than leaving them in. Even if you go to mex and use fully optimized code, I am quite certain that bothering to test for -inf and skipping it would be slower than including it.
Leaving them in is not going to affect the result compared to removing them, except in the case where all values along the dimension are -inf, and you have not defined what output you want for that.
If your portion of -inf was much higher still (e.g., 90%+) then my suggestion would be to use sparse arrays to represent the locations that are not -inf. You would still need to be careful, though, as a naive max() on a sparse array would treat the "missing" locations as 0 and 0 might happen to be larger than the actual maximum value. Using sparse arrays would not be efficient for ~50% occupancy.