## Can we sum a series of values,even some of them are "Inf"?If i want to do it,how can i do?

yang-En Hsiao

### yang-En Hsiao (view profile)

さんによって質問されました 2019 年 3 月 26 日

### Torsten (view profile)

さんによって コメントされました 2019 年 3 月 27 日
John D'Errico

### John D'Errico (view profile)

さんの 回答が採用されました
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 件のコメント

サインイン to comment.

## 1 件の回答 ### John D'Errico (view profile)

on 26 Mar 2019
Edited by John D'Errico

### John D'Errico (view profile)

on 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.)

John D'Errico

### John D'Errico (view profile)

on 26 Mar 2019
I showed you exactly how to solve the problem. Or you can save the results for each iteration as Torsten did, then form the mean of the vector of results at the end. Both approaches work with no problems. You can do the sum in the loop or outside of the loop. If you do it inside the loop, just divide by the number of elements that were found to be finite at the end. What is the problem?
yang-En Hsiao

### yang-En Hsiao (view profile)

on 27 Mar 2019
Here is my code,and i found a bug in here if i use the mean(c(~isinf(c)))
bd=2
for l=1:bd
%hat_p_up=P_p;
hat_p_up=0.0824
%OP4
%declare
K=4;
N=4;
L=5;%distance between RX & TX
xi=10^-4%tolerence between
nois_var_hk_2pow=0.1*(L^(-2.5))*10;%W,0.1*(L^(-2.5)),if this unit is dbm
w_nois_var_hk_2pow=10^(-3)*10^(0.1*nois_var_hk_2pow)
nois_var_ak_2pow=[1.0000e-10 1.0000e-10 1.0000e-10 1.0000e-10 ];
nois_var_dk_2pow=[1.0000e-08 1.0000e-08 1.0000e-08 1.0000e-08 ];
bar_r=[10 10 10 10]
P_T=10
co = zeros(2,1);
h_1=sqrt(w_nois_var_hk_2pow/2)*(randn(N,1)+1i*randn(N,1));
h_2=sqrt(w_nois_var_hk_2pow/2)*(randn(N,1)+1i*randn(N,1));
h_3=sqrt(w_nois_var_hk_2pow/2)*(randn(N,1)+1i*randn(N,1));
h_4=sqrt(w_nois_var_hk_2pow/2)*(randn(N,1)+1i*randn(N,1));
h_kk=cat(2,h_1 ,h_2 ,h_3, h_4)
for n=1:4
h_k{n}=h_kk(1:4 , n);
n=n+1;
end
%==========================
cvx_begin
variable FNNK_up(N,N,K) semidefinite;%c7
variable rho_k_up(1,1,K) semidefinite;
%==========================
Fkk_up=cat(2,FNNK_up);
up=0
for o_up=1:4
Fk_up{o_up}=Fkk_up(1:4,o_up+3*up:4*o_up)
up=up+1;
end
tr_ace_up=0
for t=1:K
tr_ace_up=tr_ace_up+trace(Fk_up{t})
end
%====================================
%object function
minimize( tr_ace_up )
%====================================
%Constraint
subject to
%c3
rho_k_up<=1;
%===================================================
%c5
c5_left_hand_up = 0;
for k = 1:K
sum_5_up = 0;
for j = 1:K
if j ~= k
sum_5_up = sum_5_up + h_k{k}' * Fk_up{j} * h_k{k};
end
end
c5_left_hand_up = c5_left_hand_up - sum_5_up+ (h_k{k}' * Fk_up{k} * h_k{k}*inv_pos(bar_r(1)))
c5_right_hand_up= nois_var_ak_2pow(1)+ ( nois_var_dk_2pow(1)*inv_pos(rho_k_up(k)) )
real( c5_left_hand_up ) >= c5_right_hand_up
end
%===================================================
%c10
c10_left_hand_up = 0;
sum_10_up = 0;
for j = 1:K
sum_10_up= sum_10_up + h_k{k}' * Fk_up{j} * h_k{k};
end
c10_left_hand_up = c10_left_hand_up + sum_10_up+nois_var_ak_2pow(1)
c10_right_hand_up=hat_p_up*inv_pos(1-rho_k_up(k))
real(c10_left_hand_up)>= c10_right_hand_up
cvx_end
end
co(bd) = cvx_optval
c_sum=sum(co(~isinf(co)))
c_mean = mean(co(~isinf(co)))
I got two cvx_optval,one is Inf and one is 538.6262,so theoretically,the c_sum should be the same as c_mean,bacause i have only one value which is not inf,but i found that c_sum=538.6262,and c_mean= 269.3131,why is that?
By the way,if you want to run my code ,you have to install the cvx into the matlab first Torsten

### Torsten (view profile)

on 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.

サインイン to comment.