How do i replace this code to fast up?
Ä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
Geoff Hayes
am 25 Aug. 2014
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
am 27 Aug. 2014
Geoff Hayes
am 27 Aug. 2014
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
am 27 Aug. 2014
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
am 27 Aug. 2014
@Iain - that is a good point. I had assumed that the poshidprobs were matrices.
@Subha - are the poshidprobsX matrices or functions?
subha
am 28 Aug. 2014
subha
am 28 Aug. 2014
Geoff Hayes
am 28 Aug. 2014
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
am 28 Aug. 2014
Geoff Hayes
am 28 Aug. 2014
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
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
Weitere Antworten (0)
Kategorien
Mehr zu Entering Commands finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!