How do i replace this code to fast up?
    4 Ansichten (letzte 30 Tage)
  
       Ältere Kommentare anzeigen
    
Variable poshidstates have 4 possible values(0,1,2,3). Everytime, poshidstates has to select 1 value out of this four.In this below code, i=100,j=500. Because of below mentioned code part , my program run more than 8 hour instead of just 1 hour(I have to call this code for 600 batches and each batch for 50 times). how can i replace this?
   val_vect=[0 1 2 3];
  for i=1:numcases;
      for j=1:numhid;
          prob=[poshidprobs0(i,j),poshidprobs1(i,j),poshidprobs2(i,j),poshidprobs3(i,j)];
          K=find(mnrnd(1,prob)==1);
          poshidstate(i,j)=val_vect(K);
      end
  end
16 Kommentare
  Iain
      
 am 28 Aug. 2014
				Ok, now I see why it takes so long. You're repeating that calculation 30,000 times.
But seeing what you're doing, this might be faster:
 temp = rand(100, 500);
 poshidstate = (temp > poshidprobs0) + (temp > (poshidprobs0 + poshidprobs1)) + (temp > (poshidprobs0 + poshidprobs1 + poshidprobs2));
Akzeptierte Antwort
  Iain
      
 am 29 Aug. 2014
        Ok, now I see why it takes so long. You're repeating that calculation 30,000 times.
But seeing what you're doing, this might be faster:
 temp = rand(100, 500);
 poshidstate = (temp > poshidprobs0) + (temp > (poshidprobs0 + poshidprobs1)) + (temp > (poshidprobs0 + poshidprobs1 + poshidprobs2));
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
				Mehr zu Entering Commands finden Sie in Help Center und File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!