MATLAB Answers

subha
0

How do i replace this code to fast up?

Asked by subha
on 25 Aug 2014
Latest activity Answered by Iain
on 29 Aug 2014

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 Comments

@Geoff. I removed whole lines whatever i posted here. Instead that i used, poshidstate = poshidprobs1+ poshidprobs2+poshidprobs3 . During that time, it took around 40 min only. It was working fine. But it is an approximation.I want to use the one,posted here. numbatches=600.

yes this is part of my program. As my code is around 100 lines long, i couldnot post everything over here. But in short, my program looks likes this.

for epoch=1:50;
   for batch= 1:numbatches;
       data = batchdata(:,:,batch)
          % calculations related to poshidprobs 0, 1, 2, 3 goes  here
           % then the posted part comes here
   end
end

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));

Hi lain, Thanks a lot..I am so happy. You saved lots and lots of time .Problem resolved. I am searching for this for long. Now its just take 40 minutes. Thank u so much.No words to express.So happy. Please post this as answer. So many will be looking for this. Thanks mathworks

Log in to comment.

Tags

Products

No products are associated with this question.

1 Answer

Answer by Iain
on 29 Aug 2014
 Accepted Answer

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 Comments

Log in to comment.


Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!