How to sum a series of values, ignoring any NaN values?Adding 2x2 matrix with NaNsHow to sum array of numbers in Ruby?What is the rationale for all comparisons returning false for IEEE754 NaN values?How do I get rid of NaNs in MATLAB?How do you check that a number is NaN in JavaScript?Is there a better way of making numpy.argmin() ignore NaN valuesHow to check if any value is NaN in a Pandas DataFrameHow to compute sum of series until the sum stops changing in MatlabTensorflow: how to handle input data with nan valuesSum a daily time series into a monthly time series with a NaN value thresholdHow to ignore NaN in rolling average calculation in Python

Does throwing a penny at a train stop the train?

How can I get a player to accept that they should stop trying to pull stunts without thinking them through first?

Is there a strong legal guarantee that the U.S. can give to another country that it won't attack them?

Credit score and financing new car

How are mathematicians paid to do research?

Managing and organizing the massively increased number of classes after switching to SOLID?

What to do with a rabbit in a survival situation?

Optimization terminology: "Exact" v. "Approximate"

For a hashing function like MD5, how similar can two plaintext strings be and still generate the same hash?

Single word for "refusing to move to next activity unless present one is completed."

What is a "shilicashe?"

Is a request to book a business flight ticket for a graduate student an unreasonable one?

Would dual wielding daggers be a viable choice for a covert bodyguard?

Can the Mage Hand cantrip be used to trip an enemy who is running away?

Adding labels to a matrix

What's the point of having a RAID 1 configuration over incremental backups to a secondary drive?

How to deal with moral/legal subjects in writing?

Word meaning to destroy books

How to know if blackberries are safe to eat?

If your plane is out-of-control, why does military training instruct releasing the joystick to neutralize controls?

Should disabled buttons give feedback when clicked?

Which star / galaxy is moving away from us the fastest?

Has anyone in space seen or photographed a simple laser pointer from Earth?

How can I fix the dull colors I am getting in Ubuntu 19.04 Terminal?



How to sum a series of values, ignoring any NaN values?


Adding 2x2 matrix with NaNsHow to sum array of numbers in Ruby?What is the rationale for all comparisons returning false for IEEE754 NaN values?How do I get rid of NaNs in MATLAB?How do you check that a number is NaN in JavaScript?Is there a better way of making numpy.argmin() ignore NaN valuesHow to check if any value is NaN in a Pandas DataFrameHow to compute sum of series until the sum stops changing in MatlabTensorflow: how to handle input data with nan valuesSum a daily time series into a monthly time series with a NaN value thresholdHow to ignore NaN in rolling average calculation in Python






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I use MATLAB version R2015a.



I get a series answer from solving the optimal problem several times, and I want to get their sum and average them. However, some of them are NaN. How do I write code to ignore those NaN and sum the others which are not NaN?










share|improve this question
























  • What have you tried? Have you heard of rmmissing or isnan?

    – Joseph Sible
    Mar 26 at 1:46











  • i have tried the for loop to sum them,or use the sum instruction directly

    – Shine Sun
    Mar 26 at 2:06











  • Possible duplicate of Adding 2x2 matrix with NaNs

    – Cris Luengo
    Mar 26 at 3:37











  • @CrisLuengo, that post is definitely related, though I feel this set of answers if more specific and updated now.

    – SecretAgentMan
    Mar 26 at 13:09






  • 1





    @SecretAgentMan: I voted to close as duplicate because that question has the same answers as his one (nansum and sum+isnan), even though the questions are not identical. This qestion is more generic, the other one is rather specific, with the cell array complicating the question. If the vote ages away, which seems likely, at least there is a link between the questions now, it will help people find answers.

    – Cris Luengo
    Mar 26 at 13:19

















1















I use MATLAB version R2015a.



I get a series answer from solving the optimal problem several times, and I want to get their sum and average them. However, some of them are NaN. How do I write code to ignore those NaN and sum the others which are not NaN?










share|improve this question
























  • What have you tried? Have you heard of rmmissing or isnan?

    – Joseph Sible
    Mar 26 at 1:46











  • i have tried the for loop to sum them,or use the sum instruction directly

    – Shine Sun
    Mar 26 at 2:06











  • Possible duplicate of Adding 2x2 matrix with NaNs

    – Cris Luengo
    Mar 26 at 3:37











  • @CrisLuengo, that post is definitely related, though I feel this set of answers if more specific and updated now.

    – SecretAgentMan
    Mar 26 at 13:09






  • 1





    @SecretAgentMan: I voted to close as duplicate because that question has the same answers as his one (nansum and sum+isnan), even though the questions are not identical. This qestion is more generic, the other one is rather specific, with the cell array complicating the question. If the vote ages away, which seems likely, at least there is a link between the questions now, it will help people find answers.

    – Cris Luengo
    Mar 26 at 13:19













1












1








1








I use MATLAB version R2015a.



I get a series answer from solving the optimal problem several times, and I want to get their sum and average them. However, some of them are NaN. How do I write code to ignore those NaN and sum the others which are not NaN?










share|improve this question
















I use MATLAB version R2015a.



I get a series answer from solving the optimal problem several times, and I want to get their sum and average them. However, some of them are NaN. How do I write code to ignore those NaN and sum the others which are not NaN?







matlab sum average nan






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 26 at 19:37









Cris Luengo

26.7k6 gold badges24 silver badges58 bronze badges




26.7k6 gold badges24 silver badges58 bronze badges










asked Mar 26 at 1:44









Shine SunShine Sun

268 bronze badges




268 bronze badges












  • What have you tried? Have you heard of rmmissing or isnan?

    – Joseph Sible
    Mar 26 at 1:46











  • i have tried the for loop to sum them,or use the sum instruction directly

    – Shine Sun
    Mar 26 at 2:06











  • Possible duplicate of Adding 2x2 matrix with NaNs

    – Cris Luengo
    Mar 26 at 3:37











  • @CrisLuengo, that post is definitely related, though I feel this set of answers if more specific and updated now.

    – SecretAgentMan
    Mar 26 at 13:09






  • 1





    @SecretAgentMan: I voted to close as duplicate because that question has the same answers as his one (nansum and sum+isnan), even though the questions are not identical. This qestion is more generic, the other one is rather specific, with the cell array complicating the question. If the vote ages away, which seems likely, at least there is a link between the questions now, it will help people find answers.

    – Cris Luengo
    Mar 26 at 13:19

















  • What have you tried? Have you heard of rmmissing or isnan?

    – Joseph Sible
    Mar 26 at 1:46











  • i have tried the for loop to sum them,or use the sum instruction directly

    – Shine Sun
    Mar 26 at 2:06











  • Possible duplicate of Adding 2x2 matrix with NaNs

    – Cris Luengo
    Mar 26 at 3:37











  • @CrisLuengo, that post is definitely related, though I feel this set of answers if more specific and updated now.

    – SecretAgentMan
    Mar 26 at 13:09






  • 1





    @SecretAgentMan: I voted to close as duplicate because that question has the same answers as his one (nansum and sum+isnan), even though the questions are not identical. This qestion is more generic, the other one is rather specific, with the cell array complicating the question. If the vote ages away, which seems likely, at least there is a link between the questions now, it will help people find answers.

    – Cris Luengo
    Mar 26 at 13:19
















What have you tried? Have you heard of rmmissing or isnan?

– Joseph Sible
Mar 26 at 1:46





What have you tried? Have you heard of rmmissing or isnan?

– Joseph Sible
Mar 26 at 1:46













i have tried the for loop to sum them,or use the sum instruction directly

– Shine Sun
Mar 26 at 2:06





i have tried the for loop to sum them,or use the sum instruction directly

– Shine Sun
Mar 26 at 2:06













Possible duplicate of Adding 2x2 matrix with NaNs

– Cris Luengo
Mar 26 at 3:37





Possible duplicate of Adding 2x2 matrix with NaNs

– Cris Luengo
Mar 26 at 3:37













@CrisLuengo, that post is definitely related, though I feel this set of answers if more specific and updated now.

– SecretAgentMan
Mar 26 at 13:09





@CrisLuengo, that post is definitely related, though I feel this set of answers if more specific and updated now.

– SecretAgentMan
Mar 26 at 13:09




1




1





@SecretAgentMan: I voted to close as duplicate because that question has the same answers as his one (nansum and sum+isnan), even though the questions are not identical. This qestion is more generic, the other one is rather specific, with the cell array complicating the question. If the vote ages away, which seems likely, at least there is a link between the questions now, it will help people find answers.

– Cris Luengo
Mar 26 at 13:19





@SecretAgentMan: I voted to close as duplicate because that question has the same answers as his one (nansum and sum+isnan), even though the questions are not identical. This qestion is more generic, the other one is rather specific, with the cell array complicating the question. If the vote ages away, which seems likely, at least there is a link between the questions now, it will help people find answers.

– Cris Luengo
Mar 26 at 13:19












3 Answers
3






active

oldest

votes


















2














You can use inbuilt functions as suggested in the above answer. If you want to know the logic and use a loop..you can follow as shown below:



A = [NaN 1 2 NaN 3 4 7 -1 NaN] ; 
count = 0 ;
thesum = 0 ;
for i = 1:length(A)
if ~isnan(A(i))
count = count+1 ;
thesum = thesum+A(i) ;
end
end





share|improve this answer


















  • 1





    Aside from this just being the same as sum(A(~isnan(A))), I don't understand what the point of the count variable is?

    – Wolfie
    Mar 26 at 10:15











  • Ohh yes forgot to add......count is used to find mean in case needed.

    – Siva Srinivas Kolukula
    Mar 26 at 12:15






  • 1





    For future readers: note this is probably the most inefficient solution on this page. MATLAB is optimised for vector operations. Loops can be helpful to foster understanding, but it's good practise to heed the advice of others here suggesting sleeker solutions.

    – Wolfie
    Mar 26 at 14:23











  • @Wolfie, thanks for keeping me in check. I guess I meant a way to get the count without looping is count = sum(~isnan(A)).

    – SecretAgentMan
    Mar 26 at 16:07











  • @Wolfie I agree with you...my intention was to show with loop for learning things. :)

    – Siva Srinivas Kolukula
    Mar 27 at 6:58


















3














Option 1: toolbox free solution using sum and isnan from base MATLAB.



A = [1 2 3 4 5 6 7 8 9 NaN];
sum(A(~isnan(A))) % No toolbox required


Option 2: nansum (see this answer from OP)
Note: nansum requires the Statistics toolbox.



nansum(A) % Requires Statistics toolbox


Code tested using MATLAB R2018b.




Update from comments

Great suggestion from @Cris Luengo for those with more recent versions. Requires no toolbox.



sum(A,'omitnan') % No toolbox required


Another suggestion from @Ben Voigt for some applications. Also requires no toolbox.



sum(A(isfinite(A))) % No toolbox required





share|improve this answer




















  • 3





    In the more recent versions of MATLAB you can also do sum(A,'omitnan').

    – Cris Luengo
    Mar 26 at 3:30











  • Better than ~isnan(A) might be isfinite(A), depending on how the other special values ought to be handled.

    – Ben Voigt
    Mar 26 at 4:32











  • Great suggestions. Will update answer to include your notes w/ attribution.

    – SecretAgentMan
    Mar 26 at 13:05


















1














You can use the omitnan argument



A = [1 2 3 4 5 6 7 8 9 NaN];
s = sum( A, 'omitnan' )


Note, this is literally the same code as used by the nansum function from the Statistics toolbox, which was introduced before R2006a, so I would think compatibility is pretty good.






share|improve this answer























  • When was it introduced? I don’t remember for sure, but I think it was after R2015a? (OP specifies this version)

    – Cris Luengo
    Mar 26 at 12:58











  • Just updated my answer based on @CrisLuengo 's comment then saw your answer here. My mistake. Should I delete that portion of my answer or just leave it @Wolfie?

    – SecretAgentMan
    Mar 26 at 13:04











  • @Cris I don't have access to that documentation, might be able to dig through change notes, or use doc sum in your version of choice to see whether it's present. As I say, it's literally the same code as nansum (see edit nansum), but I don't know if it always was.

    – Wolfie
    Mar 26 at 13:26











  • It makes sense that they would rewrite nansum to use the new feature. It likely speeds things up. Core functions are more likely to be implemented in a compiled language, and toolbox functions are more likely to be implemented in M-files (though not always the case).

    – Cris Luengo
    Mar 26 at 13:30













Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55348712%2fhow-to-sum-a-series-of-values-ignoring-any-nan-values%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














You can use inbuilt functions as suggested in the above answer. If you want to know the logic and use a loop..you can follow as shown below:



A = [NaN 1 2 NaN 3 4 7 -1 NaN] ; 
count = 0 ;
thesum = 0 ;
for i = 1:length(A)
if ~isnan(A(i))
count = count+1 ;
thesum = thesum+A(i) ;
end
end





share|improve this answer


















  • 1





    Aside from this just being the same as sum(A(~isnan(A))), I don't understand what the point of the count variable is?

    – Wolfie
    Mar 26 at 10:15











  • Ohh yes forgot to add......count is used to find mean in case needed.

    – Siva Srinivas Kolukula
    Mar 26 at 12:15






  • 1





    For future readers: note this is probably the most inefficient solution on this page. MATLAB is optimised for vector operations. Loops can be helpful to foster understanding, but it's good practise to heed the advice of others here suggesting sleeker solutions.

    – Wolfie
    Mar 26 at 14:23











  • @Wolfie, thanks for keeping me in check. I guess I meant a way to get the count without looping is count = sum(~isnan(A)).

    – SecretAgentMan
    Mar 26 at 16:07











  • @Wolfie I agree with you...my intention was to show with loop for learning things. :)

    – Siva Srinivas Kolukula
    Mar 27 at 6:58















2














You can use inbuilt functions as suggested in the above answer. If you want to know the logic and use a loop..you can follow as shown below:



A = [NaN 1 2 NaN 3 4 7 -1 NaN] ; 
count = 0 ;
thesum = 0 ;
for i = 1:length(A)
if ~isnan(A(i))
count = count+1 ;
thesum = thesum+A(i) ;
end
end





share|improve this answer


















  • 1





    Aside from this just being the same as sum(A(~isnan(A))), I don't understand what the point of the count variable is?

    – Wolfie
    Mar 26 at 10:15











  • Ohh yes forgot to add......count is used to find mean in case needed.

    – Siva Srinivas Kolukula
    Mar 26 at 12:15






  • 1





    For future readers: note this is probably the most inefficient solution on this page. MATLAB is optimised for vector operations. Loops can be helpful to foster understanding, but it's good practise to heed the advice of others here suggesting sleeker solutions.

    – Wolfie
    Mar 26 at 14:23











  • @Wolfie, thanks for keeping me in check. I guess I meant a way to get the count without looping is count = sum(~isnan(A)).

    – SecretAgentMan
    Mar 26 at 16:07











  • @Wolfie I agree with you...my intention was to show with loop for learning things. :)

    – Siva Srinivas Kolukula
    Mar 27 at 6:58













2












2








2







You can use inbuilt functions as suggested in the above answer. If you want to know the logic and use a loop..you can follow as shown below:



A = [NaN 1 2 NaN 3 4 7 -1 NaN] ; 
count = 0 ;
thesum = 0 ;
for i = 1:length(A)
if ~isnan(A(i))
count = count+1 ;
thesum = thesum+A(i) ;
end
end





share|improve this answer













You can use inbuilt functions as suggested in the above answer. If you want to know the logic and use a loop..you can follow as shown below:



A = [NaN 1 2 NaN 3 4 7 -1 NaN] ; 
count = 0 ;
thesum = 0 ;
for i = 1:length(A)
if ~isnan(A(i))
count = count+1 ;
thesum = thesum+A(i) ;
end
end






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 26 at 5:16









Siva Srinivas KolukulaSiva Srinivas Kolukula

1,2261 gold badge7 silver badges13 bronze badges




1,2261 gold badge7 silver badges13 bronze badges







  • 1





    Aside from this just being the same as sum(A(~isnan(A))), I don't understand what the point of the count variable is?

    – Wolfie
    Mar 26 at 10:15











  • Ohh yes forgot to add......count is used to find mean in case needed.

    – Siva Srinivas Kolukula
    Mar 26 at 12:15






  • 1





    For future readers: note this is probably the most inefficient solution on this page. MATLAB is optimised for vector operations. Loops can be helpful to foster understanding, but it's good practise to heed the advice of others here suggesting sleeker solutions.

    – Wolfie
    Mar 26 at 14:23











  • @Wolfie, thanks for keeping me in check. I guess I meant a way to get the count without looping is count = sum(~isnan(A)).

    – SecretAgentMan
    Mar 26 at 16:07











  • @Wolfie I agree with you...my intention was to show with loop for learning things. :)

    – Siva Srinivas Kolukula
    Mar 27 at 6:58












  • 1





    Aside from this just being the same as sum(A(~isnan(A))), I don't understand what the point of the count variable is?

    – Wolfie
    Mar 26 at 10:15











  • Ohh yes forgot to add......count is used to find mean in case needed.

    – Siva Srinivas Kolukula
    Mar 26 at 12:15






  • 1





    For future readers: note this is probably the most inefficient solution on this page. MATLAB is optimised for vector operations. Loops can be helpful to foster understanding, but it's good practise to heed the advice of others here suggesting sleeker solutions.

    – Wolfie
    Mar 26 at 14:23











  • @Wolfie, thanks for keeping me in check. I guess I meant a way to get the count without looping is count = sum(~isnan(A)).

    – SecretAgentMan
    Mar 26 at 16:07











  • @Wolfie I agree with you...my intention was to show with loop for learning things. :)

    – Siva Srinivas Kolukula
    Mar 27 at 6:58







1




1





Aside from this just being the same as sum(A(~isnan(A))), I don't understand what the point of the count variable is?

– Wolfie
Mar 26 at 10:15





Aside from this just being the same as sum(A(~isnan(A))), I don't understand what the point of the count variable is?

– Wolfie
Mar 26 at 10:15













Ohh yes forgot to add......count is used to find mean in case needed.

– Siva Srinivas Kolukula
Mar 26 at 12:15





Ohh yes forgot to add......count is used to find mean in case needed.

– Siva Srinivas Kolukula
Mar 26 at 12:15




1




1





For future readers: note this is probably the most inefficient solution on this page. MATLAB is optimised for vector operations. Loops can be helpful to foster understanding, but it's good practise to heed the advice of others here suggesting sleeker solutions.

– Wolfie
Mar 26 at 14:23





For future readers: note this is probably the most inefficient solution on this page. MATLAB is optimised for vector operations. Loops can be helpful to foster understanding, but it's good practise to heed the advice of others here suggesting sleeker solutions.

– Wolfie
Mar 26 at 14:23













@Wolfie, thanks for keeping me in check. I guess I meant a way to get the count without looping is count = sum(~isnan(A)).

– SecretAgentMan
Mar 26 at 16:07





@Wolfie, thanks for keeping me in check. I guess I meant a way to get the count without looping is count = sum(~isnan(A)).

– SecretAgentMan
Mar 26 at 16:07













@Wolfie I agree with you...my intention was to show with loop for learning things. :)

– Siva Srinivas Kolukula
Mar 27 at 6:58





@Wolfie I agree with you...my intention was to show with loop for learning things. :)

– Siva Srinivas Kolukula
Mar 27 at 6:58













3














Option 1: toolbox free solution using sum and isnan from base MATLAB.



A = [1 2 3 4 5 6 7 8 9 NaN];
sum(A(~isnan(A))) % No toolbox required


Option 2: nansum (see this answer from OP)
Note: nansum requires the Statistics toolbox.



nansum(A) % Requires Statistics toolbox


Code tested using MATLAB R2018b.




Update from comments

Great suggestion from @Cris Luengo for those with more recent versions. Requires no toolbox.



sum(A,'omitnan') % No toolbox required


Another suggestion from @Ben Voigt for some applications. Also requires no toolbox.



sum(A(isfinite(A))) % No toolbox required





share|improve this answer




















  • 3





    In the more recent versions of MATLAB you can also do sum(A,'omitnan').

    – Cris Luengo
    Mar 26 at 3:30











  • Better than ~isnan(A) might be isfinite(A), depending on how the other special values ought to be handled.

    – Ben Voigt
    Mar 26 at 4:32











  • Great suggestions. Will update answer to include your notes w/ attribution.

    – SecretAgentMan
    Mar 26 at 13:05















3














Option 1: toolbox free solution using sum and isnan from base MATLAB.



A = [1 2 3 4 5 6 7 8 9 NaN];
sum(A(~isnan(A))) % No toolbox required


Option 2: nansum (see this answer from OP)
Note: nansum requires the Statistics toolbox.



nansum(A) % Requires Statistics toolbox


Code tested using MATLAB R2018b.




Update from comments

Great suggestion from @Cris Luengo for those with more recent versions. Requires no toolbox.



sum(A,'omitnan') % No toolbox required


Another suggestion from @Ben Voigt for some applications. Also requires no toolbox.



sum(A(isfinite(A))) % No toolbox required





share|improve this answer




















  • 3





    In the more recent versions of MATLAB you can also do sum(A,'omitnan').

    – Cris Luengo
    Mar 26 at 3:30











  • Better than ~isnan(A) might be isfinite(A), depending on how the other special values ought to be handled.

    – Ben Voigt
    Mar 26 at 4:32











  • Great suggestions. Will update answer to include your notes w/ attribution.

    – SecretAgentMan
    Mar 26 at 13:05













3












3








3







Option 1: toolbox free solution using sum and isnan from base MATLAB.



A = [1 2 3 4 5 6 7 8 9 NaN];
sum(A(~isnan(A))) % No toolbox required


Option 2: nansum (see this answer from OP)
Note: nansum requires the Statistics toolbox.



nansum(A) % Requires Statistics toolbox


Code tested using MATLAB R2018b.




Update from comments

Great suggestion from @Cris Luengo for those with more recent versions. Requires no toolbox.



sum(A,'omitnan') % No toolbox required


Another suggestion from @Ben Voigt for some applications. Also requires no toolbox.



sum(A(isfinite(A))) % No toolbox required





share|improve this answer















Option 1: toolbox free solution using sum and isnan from base MATLAB.



A = [1 2 3 4 5 6 7 8 9 NaN];
sum(A(~isnan(A))) % No toolbox required


Option 2: nansum (see this answer from OP)
Note: nansum requires the Statistics toolbox.



nansum(A) % Requires Statistics toolbox


Code tested using MATLAB R2018b.




Update from comments

Great suggestion from @Cris Luengo for those with more recent versions. Requires no toolbox.



sum(A,'omitnan') % No toolbox required


Another suggestion from @Ben Voigt for some applications. Also requires no toolbox.



sum(A(isfinite(A))) % No toolbox required






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 26 at 16:13

























answered Mar 26 at 3:01









SecretAgentManSecretAgentMan

1,4509 silver badges24 bronze badges




1,4509 silver badges24 bronze badges







  • 3





    In the more recent versions of MATLAB you can also do sum(A,'omitnan').

    – Cris Luengo
    Mar 26 at 3:30











  • Better than ~isnan(A) might be isfinite(A), depending on how the other special values ought to be handled.

    – Ben Voigt
    Mar 26 at 4:32











  • Great suggestions. Will update answer to include your notes w/ attribution.

    – SecretAgentMan
    Mar 26 at 13:05












  • 3





    In the more recent versions of MATLAB you can also do sum(A,'omitnan').

    – Cris Luengo
    Mar 26 at 3:30











  • Better than ~isnan(A) might be isfinite(A), depending on how the other special values ought to be handled.

    – Ben Voigt
    Mar 26 at 4:32











  • Great suggestions. Will update answer to include your notes w/ attribution.

    – SecretAgentMan
    Mar 26 at 13:05







3




3





In the more recent versions of MATLAB you can also do sum(A,'omitnan').

– Cris Luengo
Mar 26 at 3:30





In the more recent versions of MATLAB you can also do sum(A,'omitnan').

– Cris Luengo
Mar 26 at 3:30













Better than ~isnan(A) might be isfinite(A), depending on how the other special values ought to be handled.

– Ben Voigt
Mar 26 at 4:32





Better than ~isnan(A) might be isfinite(A), depending on how the other special values ought to be handled.

– Ben Voigt
Mar 26 at 4:32













Great suggestions. Will update answer to include your notes w/ attribution.

– SecretAgentMan
Mar 26 at 13:05





Great suggestions. Will update answer to include your notes w/ attribution.

– SecretAgentMan
Mar 26 at 13:05











1














You can use the omitnan argument



A = [1 2 3 4 5 6 7 8 9 NaN];
s = sum( A, 'omitnan' )


Note, this is literally the same code as used by the nansum function from the Statistics toolbox, which was introduced before R2006a, so I would think compatibility is pretty good.






share|improve this answer























  • When was it introduced? I don’t remember for sure, but I think it was after R2015a? (OP specifies this version)

    – Cris Luengo
    Mar 26 at 12:58











  • Just updated my answer based on @CrisLuengo 's comment then saw your answer here. My mistake. Should I delete that portion of my answer or just leave it @Wolfie?

    – SecretAgentMan
    Mar 26 at 13:04











  • @Cris I don't have access to that documentation, might be able to dig through change notes, or use doc sum in your version of choice to see whether it's present. As I say, it's literally the same code as nansum (see edit nansum), but I don't know if it always was.

    – Wolfie
    Mar 26 at 13:26











  • It makes sense that they would rewrite nansum to use the new feature. It likely speeds things up. Core functions are more likely to be implemented in a compiled language, and toolbox functions are more likely to be implemented in M-files (though not always the case).

    – Cris Luengo
    Mar 26 at 13:30















1














You can use the omitnan argument



A = [1 2 3 4 5 6 7 8 9 NaN];
s = sum( A, 'omitnan' )


Note, this is literally the same code as used by the nansum function from the Statistics toolbox, which was introduced before R2006a, so I would think compatibility is pretty good.






share|improve this answer























  • When was it introduced? I don’t remember for sure, but I think it was after R2015a? (OP specifies this version)

    – Cris Luengo
    Mar 26 at 12:58











  • Just updated my answer based on @CrisLuengo 's comment then saw your answer here. My mistake. Should I delete that portion of my answer or just leave it @Wolfie?

    – SecretAgentMan
    Mar 26 at 13:04











  • @Cris I don't have access to that documentation, might be able to dig through change notes, or use doc sum in your version of choice to see whether it's present. As I say, it's literally the same code as nansum (see edit nansum), but I don't know if it always was.

    – Wolfie
    Mar 26 at 13:26











  • It makes sense that they would rewrite nansum to use the new feature. It likely speeds things up. Core functions are more likely to be implemented in a compiled language, and toolbox functions are more likely to be implemented in M-files (though not always the case).

    – Cris Luengo
    Mar 26 at 13:30













1












1








1







You can use the omitnan argument



A = [1 2 3 4 5 6 7 8 9 NaN];
s = sum( A, 'omitnan' )


Note, this is literally the same code as used by the nansum function from the Statistics toolbox, which was introduced before R2006a, so I would think compatibility is pretty good.






share|improve this answer













You can use the omitnan argument



A = [1 2 3 4 5 6 7 8 9 NaN];
s = sum( A, 'omitnan' )


Note, this is literally the same code as used by the nansum function from the Statistics toolbox, which was introduced before R2006a, so I would think compatibility is pretty good.







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 26 at 10:14









WolfieWolfie

17.4k6 gold badges17 silver badges46 bronze badges




17.4k6 gold badges17 silver badges46 bronze badges












  • When was it introduced? I don’t remember for sure, but I think it was after R2015a? (OP specifies this version)

    – Cris Luengo
    Mar 26 at 12:58











  • Just updated my answer based on @CrisLuengo 's comment then saw your answer here. My mistake. Should I delete that portion of my answer or just leave it @Wolfie?

    – SecretAgentMan
    Mar 26 at 13:04











  • @Cris I don't have access to that documentation, might be able to dig through change notes, or use doc sum in your version of choice to see whether it's present. As I say, it's literally the same code as nansum (see edit nansum), but I don't know if it always was.

    – Wolfie
    Mar 26 at 13:26











  • It makes sense that they would rewrite nansum to use the new feature. It likely speeds things up. Core functions are more likely to be implemented in a compiled language, and toolbox functions are more likely to be implemented in M-files (though not always the case).

    – Cris Luengo
    Mar 26 at 13:30

















  • When was it introduced? I don’t remember for sure, but I think it was after R2015a? (OP specifies this version)

    – Cris Luengo
    Mar 26 at 12:58











  • Just updated my answer based on @CrisLuengo 's comment then saw your answer here. My mistake. Should I delete that portion of my answer or just leave it @Wolfie?

    – SecretAgentMan
    Mar 26 at 13:04











  • @Cris I don't have access to that documentation, might be able to dig through change notes, or use doc sum in your version of choice to see whether it's present. As I say, it's literally the same code as nansum (see edit nansum), but I don't know if it always was.

    – Wolfie
    Mar 26 at 13:26











  • It makes sense that they would rewrite nansum to use the new feature. It likely speeds things up. Core functions are more likely to be implemented in a compiled language, and toolbox functions are more likely to be implemented in M-files (though not always the case).

    – Cris Luengo
    Mar 26 at 13:30
















When was it introduced? I don’t remember for sure, but I think it was after R2015a? (OP specifies this version)

– Cris Luengo
Mar 26 at 12:58





When was it introduced? I don’t remember for sure, but I think it was after R2015a? (OP specifies this version)

– Cris Luengo
Mar 26 at 12:58













Just updated my answer based on @CrisLuengo 's comment then saw your answer here. My mistake. Should I delete that portion of my answer or just leave it @Wolfie?

– SecretAgentMan
Mar 26 at 13:04





Just updated my answer based on @CrisLuengo 's comment then saw your answer here. My mistake. Should I delete that portion of my answer or just leave it @Wolfie?

– SecretAgentMan
Mar 26 at 13:04













@Cris I don't have access to that documentation, might be able to dig through change notes, or use doc sum in your version of choice to see whether it's present. As I say, it's literally the same code as nansum (see edit nansum), but I don't know if it always was.

– Wolfie
Mar 26 at 13:26





@Cris I don't have access to that documentation, might be able to dig through change notes, or use doc sum in your version of choice to see whether it's present. As I say, it's literally the same code as nansum (see edit nansum), but I don't know if it always was.

– Wolfie
Mar 26 at 13:26













It makes sense that they would rewrite nansum to use the new feature. It likely speeds things up. Core functions are more likely to be implemented in a compiled language, and toolbox functions are more likely to be implemented in M-files (though not always the case).

– Cris Luengo
Mar 26 at 13:30





It makes sense that they would rewrite nansum to use the new feature. It likely speeds things up. Core functions are more likely to be implemented in a compiled language, and toolbox functions are more likely to be implemented in M-files (though not always the case).

– Cris Luengo
Mar 26 at 13:30

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55348712%2fhow-to-sum-a-series-of-values-ignoring-any-nan-values%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript