how can i repeat the function using iteration
    4 ビュー (過去 30 日間)
  
       古いコメントを表示
    
hello , i have the below code , and i want to repeat it using 30 iteration , in each iteration it will give me different value of (MaxRwrd), so when it go to the second iteration  if the value of the current MaxRwrd is larger than the previous one so set the value of the MaxRwrd to be of the second iteration , otherwise keep the value of the MaxRwrd as the previous iteration , then increase the number of iterations and so on untill it reach the maximum value which is 30, anyone can help?
D = zeros(size(L));
for v = 1:N   
  idx = find(L(v,:)==1);
  n = numel(idx);
  if n ~= 0 
    in = randi(n);
    D(v,idx(in)) = 1.0 
     for m=1:M    
       for i=n:N
          if D(v,m)==1 && c(v,i,m)==0
                  D(v,m)=1;   
                  Rwr(v,m)=Rwrd(v,m);
              elseif D(v,m)==1 && c(v,i,m)==1
                  D(v,m)=1;
                  L(i,m)=0;
                  Rwr(v,m)=Rwrd(v,m);
          else  D(v,m)=0;
                Rwr(v,m)=0;
          end
       end
     end
  end
end    
DD=D
Reward=Rwr
MaxRwrd=sum(sum(Rwr))
0 件のコメント
回答 (1 件)
  Torsten
      
      
 2022 年 6 月 5 日
        
      編集済み: Torsten
      
      
 2022 年 6 月 5 日
  
      MAXRWRD = -Inf;
itermax = 30;
iter = 0;
while MAXRWRD < 30 && iter < itermax
    iter = iter + 1;
    D = zeros(size(L));
    for v = 1:N   
        idx = find(L(v,:)==1);
        n = numel(idx);
        if n ~= 0 
            in = randi(n);
            D(v,idx(in)) = 1.0 
            for m=1:M    
                for i=n:N
                    if D(v,m)==1 && c(v,i,m)==0
                        D(v,m)=1;   
                        Rwr(v,m)=Rwrd(v,m);
                    elseif D(v,m)==1 && c(v,i,m)==1
                        D(v,m)=1;
                        L(i,m)=0;
                        Rwr(v,m)=Rwrd(v,m);
                    else  D(v,m)=0;
                        Rwr(v,m)=0;
                    end
                end
            end
        end
    end    
    DD=D
    Reward=Rwr
    MaxRwrd=sum(sum(Rwr))
    MAXRWRD = max(MAXRWRD,MaxRwrd);
end
MAXRWRD
iter
参考
カテゴリ
				Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

