[Solved] switching numbers in matlab [closed]


I am not really sure that I got all of your requirements but this might be the script you are searching for:

N = 10;     % count of numbers
p = 0.2;    % switching probability
a = 5;
b = 20; 

% init empty numbers and get the first random number
numbers = zeros(N,1);
numbers(1) = rand(1) * (b-a) + a;

for iNumber = 2:N
    % test if number must change
    if rand(1) < (1-p)
        % the number must be the same than the previous
        % copy the value and go to next number
        numbers(iNumber) = numbers(iNumber-1);
        continue;

    else 
        % a new number must be found
        while 1
            % get a new random number 
            numbers(iNumber) = rand(1) * (b-a) + a;

            % check if the new random number is different from the previous
            if numbers(iNumber) ~= numbers(iNumber-1)
                % in case they are different, the while 1 can be stopped
                break;
            end

        end % while 1

    end % if rand(1) < (1-p)
end % for iNumber = 2:N

1

solved switching numbers in matlab [closed]