How do i replace this code to fast up?
이전 댓글 표시
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
Geoff Hayes
2014년 8월 25일
Is it always guaranteed that prob has an element that is equal to one? And that it has at most one element like this?
As for speeding it up, have you pre-allocated memory to poshidstate? Just prior to entering the for loop do
poshidstate = zeros(numcases,numhid);
Though, to be honest, I don't think that this pre-allocation will reduce the running time by all that much.
subha
2014년 8월 27일
Geoff Hayes
2014년 8월 27일
Subha - where in the code are you spending the eight hours? I don't have the Statistics Toolbox so can't observe how expensive mrnd is, but running the above with your dimensions of i==100,j=500, takes around a second.
So out of curiosity if you replace
K=find(mnrnd(1,prob)==1);
with
K=1;
how long does it take to run the code?
And as Salaheddin commented, have you verified that the result returned by
K=find(mnrnd(1,prob)==1);
is in fact a 1x1 scalar, and not a vector of multiple indices?
Iain
2014년 8월 27일
The only two things I can think of that would make this code so slow is poshidprobs0,1 2 & 3 being anonymous functions, or you're running out of RAM.
Geoff Hayes
2014년 8월 27일
@Iain - that is a good point. I had assumed that the poshidprobs were matrices.
@Subha - are the poshidprobsX matrices or functions?
subha
2014년 8월 28일
subha
2014년 8월 28일
Geoff Hayes
2014년 8월 28일
Subha - when you say When i remove this part from my code and run with some approximation, what part of the code have you removed? Please describe the line.
And, what are poshidprobs0, poshidprobs1, poshidprobs2, and poshidprobs4? Are they matrices or function calls?
subha
2014년 8월 28일
Geoff Hayes
2014년 8월 28일
Subha - I missed from your previous comment that the poshidprobsX are matrices.
But when you say When i remove this part from my code and run with some approximation, what part of the code have you removed? Please describe the line, and the approximation.
Given how you have mentioned batches and epochs, I get the feeling that we are not seeing all of your code, only a piece of it...
Iain
2014년 8월 28일
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개)
카테고리
도움말 센터 및 File Exchange에서 Entering Commands에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!