hypothetical matlab's limit

Hi everyone. I have to calculate a sumof a vector of doubles of 1x4884 components. When I use this function i get as ans=nan. I there a limit to the maximum number that matlab is able to calculate? If yes how can i change this cobstraint?
Thank you in advance

1 Comment

Without knowing what your data is , it's hard to answer.

Sign in to comment.

Answers (1)

Adam Danz
Adam Danz on 9 Feb 2019
Edited: Adam Danz on 9 Feb 2019
I'm sure there is such a limit in Matlab but it's nowhere near a vector length of 4884.
The problem is that you have at least 1 NaN value in your data.
sum([1, NaN])
ans =
NaN
Use the 'omitnan' flag to ignore NaN values.
sum([1,NaN], 'omitnan')
ans =
1
To confirm that you have at least one NaN value in your data, if this next line returns True (1), you have NaN value(s).
any(isnan(yourData)) % to find where the NaNs are: find(isnan(yourData))
And here's a test to confirm that Matlab can handle vector lengths of 4884 (or even 4884000 for that matter).
sum(rand(1, 4884))

7 Comments

Yeah, good call.
Replace with 0s
yourData(isnan(yourData)) = 0;
sum(yourData)
or just discount NaNs
sum(yourData(~isnan(yourData)))
+1. But just how large would that limit be? :)
(I think the longest vector I have ever summed in MATLAB probably had on the order of a billion elements. Not a problem. But it probably did kick my system fan on high.)
The length of a vector allowed in MATLAB is HUGE. Vastly larger than the amount of memory any computer will be able to provide, even if you go into virtual memory on your disk. So you will run out of memory far sooner than you will no longer be able to form a vector big enough to be a problem. That of course will cause different problems, different errors. But to create a NaN?
It would seem difficult to get a NaN from a sum of finite numbers. For example,
realmax
ans =
1.79769313486232e+308
realmax + realmax
ans =
Inf
sum(repmat(realmax,1,10000))
ans =
Inf
So numbers that are larger than realmax overflow into inf. But not into NaN. Even if we tried to form the sum of a vector of length 1e309, composed of ones, the result would be only 2^53.
2^53 + 1 + 1 + 1 + 1 + 1 + 1 == 2^53
ans =
logical
1
We cannot add more than 2^53 ones together without running into a limit at flintmax, because each time we add one more 1 to that result, it does not increase the sum.
But there are indeed two ways to get a NaN from a sum. One is if the elements of the sum include a NaN. A second is if there are two or more infs in the set, of different signs.
inf + inf
ans =
Inf
So we can add as many infs together as we wish. But the DIFFERENCE of infs is indeterminate, and therefore results in a NaN.
inf + -inf
ans =
NaN
So you also need to worry about infs in your data. And once you get even one NaN in your data, they proliferate wildly, breeding like bunnies in the springtime, or coat hangers in your closet.
That means you want to use tools like the 'omitnan' flag to sum (or in older releases of MATLAB, tools like nanmean or nansum.) You canalso use dbstop to indicate when aNaN or inf as occurred. So you can set the debugger to trap out when a NaN or inf arises.
dbstop if naninf
Nice info! I wasn't aware about the sum([-inf, inf]).
The trick to remember is anytime you have an indeterminate result, you should get NaN.
So inf+inf is just bigger than inf, but nothing is bigger than inf. Even inf^inf does not roll into a NaN.
inf^inf
ans =
Inf
inf^(-inf)
ans =
0
I was actually wondering if that latter one would go into a NaN, but it did not, nor did these:
inf^0
ans =
1
0^inf
ans =
0
The NaN producers I can think of are...
inf - inf
ans =
NaN
inf/inf
ans =
NaN
0*inf
ans =
NaN
sin(inf)
ans =
NaN
cos(inf)
ans =
NaN
tan(inf)
ans =
NaN
And of course the classic:
0/0
ans =
NaN
This Matlab summary covers most of your examples and also mentions rem(x,y) when x is inf or y is 0.
Yes. rem also makes sense.

Sign in to comment.

Categories

Asked:

on 9 Feb 2019

Commented:

on 9 Feb 2019

Community Treasure Hunt

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

Start Hunting!