How to store value for multiple iteration

1 view (last 30 days)
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.

Accepted Answer

A Jenkins
A Jenkins on 23 Oct 2013
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 Comments
Eagle
Eagle on 23 Oct 2013
Edited: Eagle on 23 Oct 2013
@ A Jenkins- if s=[100 x 57] matrix then i cannot find the value of V. Please need your help.
A Jenkins
A Jenkins on 23 Oct 2013
Edited: A Jenkins on 23 Oct 2013
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)

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!