24
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 jobs from queue and add new ones to the queue. If my recursive function calls itself multiple times (e.g walking a tree with many branches) multiple jobs are added. Pseudo code:
queue = new Queue();
queue.put(param);
result = 0;
while (!queue.isEmpty()) {
param = queue.remove();
// process param and obtain new param(s)
// change result
queue.add(param1);
queue.add(param2);
}
return result;
I cannot find any queue like structure in MATLAB though. I can use vector to simulate queue where adding 3 to queue is like:
a = [a 3]
and removing element is
val = a(1);
a(1) = [];
If I got the MATLAB way right, this method will be a performance killer.
Is there a sane way to use a queue in MATLAB?
What about other data structures?