I truely want to understand the functionalities of the highlighted words in the code for better appreciation of the code

2 次查看(过去 30 天)
phh=zeros(10,10); pmm=zeros(10,10); pmh=zeros(10,10); phm=zeros(10,10); *M=zeros(10,10); H=zeros(10,10)*; source=zeros(10,200,100); *avgT=zeros(10,10); eval= zeros(1, 100); sim = zeros(10, 1)*; *se=zeros(10,1)*; *ce =zeros(10,1)*; *pc= zeros(10,100); c=zeros(10,100); T=zeros(100,100,10); actM=[1 1 1 1 1 0 0 0 0 0]; %cw2= [ 1 0 0 0 0 1 1 1 1 1] ; tq=zeros(10,T); cw3=zeros(1,100)*; q=10; while q <50 d=1; T=zeros(10,100,100); while d <5 %%%%%%%%%%%%%%%%%%%iterating over percentage of malicious packets
phh=zeros(10,10); pmm=zeros(10,10); pmh=zeros(10,10); phm=zeros(10,10); M=zeros(10,10); H=zeros(10,10); iter=1; while iter < 10 %%%%%%look into the following code, it does not run for iterations %%%%%%%%%%%%%%%%generating multiple runs for the given percentage %%%%%%%%%%%%%%%%of malicious packets
eval=zeros(1,100);
rp=randperm(100);
eval(rp(1:q)) = 1;
cw1=bsc(eval,0.04);
cw2=zeros(1,100);
%%%%%%%%%%%formulating malicious behavior
% for i = 1:100
%if i < q
% if eval(i)==1
% cw2(i)=0;
% else
% cw2(i)=1;
% end
% else
% cw2(i)=eval(i);
% end
% end
cw2=bsc(eval,(q/100));
source=zeros(100,200,100);
sim = zeros(10, 1); se=zeros(10,1); ce =zeros(10,1);
pc= zeros(10,100);
c=zeros(10,100);
for i=1:100
for j=1:10
source(j,i,:) =eval;% cw1;
if rem(i,10)<(d )&& j<((10/2) +1) &&i>1 %%%%%%%%%%%%%%%%%%%%malicious behavior %%%%%%%%%%%%%%
source(j,i,:)=cw2;%bsc(eval,q/100);
end
% if rem(i,10)<d &&j>3&&j<6 %%%%%%%%%%%%%%to have different amounts
% of misbehavior
% source(j,i,:)=cw3;
% end
for k=1:100
if (i ==1)
c(j,k)=1;
end
if (i>1&& source(j,i,k) == source(j,i-1,k))
c(j,k) =c(j,k)+1;
end
end
sim(j)=similar(source(j,i,:),eval);
se(j)= -sim(j)*log10(sim(j))/log10(100);
if isnan(se(j))
se(j)=1;
end
pc(j,:)=c(j,:)/i; pc(1,:)=c(1,:)/i;
sum =0;
for k=1:100
sum=(-pc(j,k)*log10(pc(j,k))/log10(100) )+ sum; % computing the entropy of each source for the whole code word
end
ce(j) =sum;
if isnan(ce(j))
ce(j)=1;
end
if i <2
T(d,i,j)=1-se(j); %%%%%%%%d is replaced by d
else
T(d,i,j)=(1-(se(j)+ce(j)))*0.5+ 0.5*T(d,i-1,j);
T(d,i,j)= T(d,i,j)/max(T(d,:,j));
end
if (isnan(T(d,i,j)) )
T(d,i,j)=0;
end
if (T(d,i,j)>1)
T(d,i,j)=1;
end
if (T(d,i,j)<0)
T(d,i,j)=0;
end
end
end
for i = 1: 10 % d*10 to 10 avgT(d,i)=mean(T(d,:,i)); %%%%%%%%%%replacing 'd 'by d if avgT(d,i)<0.75 M(d,i)=M(d,i)+1; if actM(i)==1 pmm (q/10,d)= pmm(q/10,d)+1; else pmh(q/10,d) =pmh(q/10,d)+1; end else H(d,i)=H(d,i)+1; if actM(i)==0 phh(q/10,d)=phh(q/10,d)+1; else phm(q/10,d) =phm(q/10,d)+1; end end end iter =iter+1; end % tp(q/10,d)=pmm(q/10,d)/(pmm(q/10,d)+phm(q/10,d)); tp(q/10,d)=pmm(q/10,d)/(pmm(q/10,d)+phm(q/10,d)); % fp(q/10,d)=pmh(q/10,d)/(phh(q/10,d)+pmh(q/10,d)); fp(q/10,d)=pmh(q/10,d)/(phh(q/10,d)+pmh(q/10,d)); d=d+1;
end
q =q+10; x=1:100; figure(q/10) plot(x,T(1,:,9),'-ob', x,T(1,:,2),'-dr', x, T(2,:,2),'-sg',x,T(3,:,2),'-m*',x,T(5,:,2),'-k^'); legend('Honest', 'Malicious, p =0.1, 10% recommendations are false', 'Malicious,p=0.2, 10% recommendations are false','Malicious,p=0.3, 10% recommendations are false','Malicious,p=0.5, 10% recommendations are false') xlabel('Iteration'); ylabel('RecommendationTrust'); title('Impact of size of recommendation vector');

回答(2 个)

KSSV
KSSV 2016-11-12
It is called initialization.....when you are going to store the output/result inside the loop, we initialization the respective arrays/Matrices to zeros. This will increase the speed of the code as the memory is already allocated to the result. If there is no initialization code will take long time to run. You can see the difference by commenting those lines and running the code.

elvin eziama
elvin eziama 2017-2-5
编辑:Walter Roberson 2017-2-5
for k =1:100
if (i==1)
c(j,k) =1;
if (i>1&& source(j,i,k)== source(j,i-1,k)
c(j,k) = c(j,k)+1
what is the idea behind i>1&& source(j,i,k)== source(j,i-1,k) more especial the i-1
  1 个评论
Walter Roberson
Walter Roberson 2017-2-5
Run-length encoding of rows. As long as the current location keeps the same value, just increment a counter. When the current location is not the same as the one before, you are just past the end of a run.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by