How to increase speed when taking the max from multidimensional array with -inf?
Show older comments
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!
Accepted Answer
More Answers (1)
James Tursa
on 23 Aug 2016
Edited: James Tursa
on 23 Aug 2016
So, your examples are not the same size for one:
N=ones(1,3,2); % <-- Not the same size as your inf example
Shouldn't this be:
N=ones(1,3,1000000);
Also, note that timing of examples like this can be heavily influenced by caching effects etc. For example, you can see a wide difference in timing of the first tic-toc results compared the the last three in this variation of your code:
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=ones(1,3,1000000);
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
tic
max(M,[],3)
toc
tic
max(N,[],3)
toc
In fact, simply reversing the order of the M vs N code will change your results, so you would have been wondering how to speed up your code by somehow including -inf's in it!
Bottom line is don't worry about it.
3 Comments
matlabstarter
on 23 Aug 2016
Edited: Walter Roberson
on 23 Aug 2016
James Tursa
on 23 Aug 2016
Ah. Got it, now. So you have a huge percentage of -inf that you know(?) will not be needed for the calculation and you were wondering if there was any way to speed up the calculation. Do you know where all of these non-inf spots are, or will you need to find them?
matlabstarter
on 23 Aug 2016
Categories
Find more on Categorical Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!