How to store value for multiple iteration

1 次查看(过去 30 天)
a= 10;
b= [1 0 0 0 1 0 1 0 1 0;1 1 0 0 1 0 1 0 1 1;1 0 1 0 1 0 1 1 1 0;1 1 1 1 1 0 1 0 1 0;1 0 1 0 1 0 1 0 1 0 ];
e= [0.05 0.08 0.2 0.4];
iteration= 1;
x= zeros(1,1);
g= eye(10);
G= [g;b];
for t=1 : iteration
s=zeros(4,1);
offset=1;
for u=e(1:length(e))
F = G ;
for i=1:15
if(rand < u)
F(i,:) = 0;
end
end
soup=zeros(1,a);
for k = 1 : 15
FD = max( F(k,:)-soup, 0) ;
if( sum(FD) == 1)
[MaxValue Idx] = max(FD) ;
soup(Idx) = 1 ;
end
end
h =sum(soup) ;
s(offset,:)=h;
offset=offset+1;
end
end
Now I want to change the value of “iteration” and want to make it 1000; For, each “iteration”
I want to count how many values of “s” are equal to “a”. Suppose, for each “iteration” the number of values in “s” that are equal to “a” is “T”(Let) Then I want to divide “T” by length(e) for each iteration. Suppose that value for each iteration is V (Let) Then I want to get the average value of V for total thousand “iteration” Example: Let, for 2 iteration, the number of values in “s” that are equal to “a” is 3,2 So,
For iteration=1, V=3/length(e)=3/4=0.75
For iteration = 2, V=2/length(e)=0.5
So, average value of V for two iteration = (0.75+0.5) / 2 = 0.625
I tried for several times but unable to do so.
Matlab experts please need your help and suggestion.

采纳的回答

A Jenkins
A Jenkins 2013-10-23
a= 10;
b= [1 0 0 0 1 0 1 0 1 0;1 1 0 0 1 0 1 0 1 1;1 0 1 0 1 0 1 1 1 0;1 1 1 1 1 0 1 0 1 0;1 0 1 0 1 0 1 0 1 0 ];
e= [0.05 0.08 0.2 0.4];
iteration= 1000;
x= zeros(1,1);
g= eye(10);
G= [g;b];
V=zeros(1,iteration);
for t=1 : iteration
s=zeros(4,1);
offset=1;
for u=e(1:length(e))
F = G ;
for i=1:15
if(rand < u)
F(i,:) = 0;
end
end
soup=zeros(1,a);
for k = 1 : 15
FD = max( F(k,:)-soup, 0) ;
if( sum(FD) == 1)
[MaxValue Idx] = max(FD) ;
soup(Idx) = 1 ;
end
end
h =sum(soup) ;
s(offset,:)=h;
offset=offset+1;
end
T=sum(s==a);
V(t)=T/length(e);
end
avgV=mean(V)
  2 个评论
Eagle
Eagle 2013-10-23
编辑:Eagle 2013-10-23
@ A Jenkins- if s=[100 x 57] matrix then i cannot find the value of V. Please need your help.
A Jenkins
A Jenkins 2013-10-23
编辑:A Jenkins 2013-10-23
sum() itself sums over one dimension. If you have a two dimensional matrix, you need to sum twice.
T=sum(sum(s==a))
Another option, if the dimensions of s can change:
T=sum(reshape(s,1,[])==a)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Simulation, Tuning, and Visualization 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by