是否有“队列"?在 MATLAB 中? [英] Is there a "queue" in MATLAB?

查看:27
本文介绍了是否有“队列"?在 MATLAB 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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?

解决方案

If you insist on using proper data structures, you can use Java from inside MATLAB:

import java.util.LinkedList
q = LinkedList();
q.add('item1');
q.add(2);
q.add([3 3 3]);
item = q.remove();
q.add('item4');

这篇关于是否有“队列"?在 MATLAB 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆