Is there a “queue” in MATLAB?

后端 未结 7 1823
温柔的废话
温柔的废话 2020-12-28 16:14

I want to convert a recursive function to a iterative one. What I normally do is, I initialize a queue, put the first job into queue. Then in a while loop I consume

相关标签:
7条回答
  • 2020-12-28 16:56

    If you can do with a FIFO queue of predefined size without the need for simple direct access, you can simply use the modulo operator and some counter variable:

    myQueueSize = 25;                  % Define queue size
    myQueue = zeros(1,myQueueSize);    % Initialize queue
    
    k = 1                              % Counter variable
    while 1                            
        % Do something, and then
        % Store some number into the queue in a FIFO manner
        myQueue(mod(k, myQueueSize)+1) = someNumberToQueue;
    
        k= k+1;                       % Iterate counter
    end
    

    This approach is super simple, but has the downside of not being as easily accessed as your typical queue. In other words, the newest element will always be element k, not element 1 etc.. For some applications, such as FIFO data storage for statistical operations, this is not necessarily a problem.

    0 讨论(0)
提交回复
热议问题