matlab - parallel independent iteration of elements in an array -
i want iterate elements independently (same condition elements though). want stop iteration when value stops changing. post part of code. according research, figured can done using parfor loop don't know how implement it. can please correct code? in advance.
probability = (ones(1,2048) .* 1/2048); tij = sum(statetransitionfwd); %gives array of 2048 elements. probability = ((probability * statetransitionbwd) - (tij .* probability)); threshold = (ones(1,2048) .* 0.05); old = zeros(1,2048); new = zeros(1,2048); while(1) probability = ((probability * statetransitionbwd) - (tij .* probability)); new = probability; if old-new <= threshold break end old = probability; end
so want steady state probability (where not changing anymore)
for 1 cannot parallellise while loops.
to implement parfor
loop make sure of following: - variables must defined before parfor loop. define output variables before loop. - iterations must absolutely independent of 1 another.
the second condition not fulfilled in case, since updating variable new
, making not independent.
i'd not parallellise code, since it's designed break before went through elements, sake of argument i'll try this:
parfor ii = 1:length(tij) probability{ii} = ((probability * statetransitionbwd) - (tij .* probability)); end
either use cell or matrix, whichever more useful work. can go ahead , use simple if
find threshold:
for ii = 1:length(probability)-1 if probability(ii+1)-probability(ii)<threshold break end end
as while loop stop condition: it's dangerous let loop run uncontrolled condition, do. it's rather easy stuck in infinite loop. use additional condition maxit
maximum number of iterations before terminating, regardless of other condition.
while probability<threshold && maxit<somenumber code end
also checking validity of full array.
a = logical([1;0]); while b=1; end c = logical([1;1]); while c d=1; end
the first while loop not run, since 1 of entries not true; second loop infinite loop now. if want terminate each row condition met, put while loop inside loop on each row instead. forloop can parallellised if put while loop function, see parfor documentation that.
Comments
Post a Comment