Hi all,
I have a small function, which generates random numbers in a specific interval. The function tests the generated values in respect to a certain bound and recalculates the random numbers again, if the bound is violated. I have to call this function several times (in this case 60 000 times), so this takes a lot of time (3/4 of the total time). I have read about vectorizing the code to improve performance. But I am totally helpless with this task. Your help would be greatly appreciated.
However, if someone has an idea to rewrite the while loop in a different way that would be great, as well.
Michi
Code:
meanValue = 10;devValue = 1;numValues = 10000;presumption = 1;for i = 1:60000 arrayValues = RandomNums(meanValue, devValue, numValues, presumption);endfunction arrayValues = RandomNums (meanValue, devValue, numValues, presumption)%% create rand nums
% normally distributed with meanValue +/- devValue
numValues = round(numValues); arrayValues = devValue*randn(numValues,1) + meanValue; % check confidence-limes for each value and create new one if outside
for idx = 1:numel(arrayValues) while (arrayValues(idx) > (meanValue + presumption*devValue) || arrayValues(idx) < (meanValue - presumption*devValue)) arrayValues(idx) = devValue*randn(1,1,'double') + meanValue; end endend
Performance:
Best Answer