Can we sum a series of values,even some of them are "Inf"?If i want to do it,how can i do?
    11 visualizzazioni (ultimi 30 giorni)
  
       Mostra commenti meno recenti
    
    yang-En Hsiao
 il 26 Mar 2019
  
    
    
    
    
    Commentato: Torsten
      
      
 il 27 Mar 2019
            I get a series answer of optimal problem in several times,and i want to sum of them and average them,however, some of them are "Inf",can i or how to write a code to ignore those "Inf" and sum the others which is not "Inf" ?
Also,if i can sum them when some of them are "Inf",and don't know the amount of value which is "Inf",do we have some method to average them,i mean,ignore the "Inf" and average the others?
The version of matlab is 2015a
0 Commenti
Risposta accettata
  John D'Errico
      
      
 il 26 Mar 2019
        
      Modificato: John D'Errico
      
      
 il 26 Mar 2019
  
      No. You cannot add numbers if some of them are infs, at least not without getting an inf as a result. OR you might even get a NaN, since inf+ -inf will result in a NaN.
sum([1:5,inf,6,7,-inf])
ans =
   NaN
but
sum([1:5,inf,6,7,inf])
ans =
   Inf
You also cannot know that some infs are bigger than other infs, as I think you may have asked.
If you want to form a sum of numbers, excluding infs, this is trivial:
V = [1:5,inf,6,7,-inf];
sum(V(~isinf(V)))
ans =
    28
Also, if you have a reasonably recent MATLAB release, you will find that tools like mean allow you to ignore NaNs in the data as an option. So you could simply convert all infs into NaNs, then use mean with the correct option set. (Or use nanmean in older releases.)
9 Commenti
  Torsten
      
      
 il 27 Mar 2019
				co is a vector with elements 0 and 538.6262. Taking the mean of this vector gives (0+538.6262)/2 = 269.3131, taking the sum of this vector gives 0+538.6262 = 538.6262.
Più risposte (0)
Vedere anche
Categorie
				Scopri di più su Get Started with Optimization Toolbox in Help Center e File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
