Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a "queue" in MATLAB?

Tags:

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?

like image 266
nimcap Avatar asked Nov 10 '10 07:11

nimcap


People also ask

What is the use of \t in Matlab?

Accepted Answer The ability to use the "\t" format in a listbox in MATLAB to place a tab in a string expression is not available. As a workaround, you can use spaces along with a fixed-width font to align the columns.

How do you make a stack in Matlab?

To create multiple stacked variables in S , use a cell array to specify multiple groups of variables from U . You can use a cell array to contain multiple values for vars , and a cell array of character vectors or string array to contain multiple values for the 'NewDataVariableName' name-value pair argument.

How do you split a script in Matlab?

Description. x = A ./ B divides each element of A by the corresponding element of B . The sizes of A and B must be the same or be compatible. If the sizes of A and B are compatible, then the two arrays implicitly expand to match each other.

What does \n mean Matlab?

\n means new line %s means print a string tt can be a string,vector or array.


2 Answers

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'); 
like image 191
Amro Avatar answered Oct 01 '22 16:10

Amro


Ok, here's a quick-and-dirty, barely tested implementation using a MATLAB handle class. If you're only storing scalar numeric values, you could use a double array for "elements" rather than a cell array. No idea about performance.

classdef Queue < handle     properties ( Access = private )         elements         nextInsert         nextRemove     end      properties ( Dependent = true )         NumElements     end      methods         function obj = Queue             obj.elements = cell(1, 10);             obj.nextInsert = 1;             obj.nextRemove = 1;         end         function add( obj, el )             if obj.nextInsert == length( obj.elements )                 obj.elements = [ obj.elements, cell( 1, length( obj.elements ) ) ];             end             obj.elements{obj.nextInsert} = el;             obj.nextInsert = obj.nextInsert + 1;         end         function el = remove( obj )             if obj.isEmpty()                 error( 'Queue is empty' );             end             el = obj.elements{ obj.nextRemove };             obj.elements{ obj.nextRemove } = [];             obj.nextRemove = obj.nextRemove + 1;             % Trim "elements"             if obj.nextRemove > ( length( obj.elements ) / 2 )                 ntrim = fix( length( obj.elements ) / 2 );                 obj.elements = obj.elements( (ntrim+1):end );                 obj.nextInsert = obj.nextInsert - ntrim;                 obj.nextRemove = obj.nextRemove - ntrim;             end         end         function tf = isEmpty( obj )             tf = ( obj.nextRemove >= obj.nextInsert );         end         function n = get.NumElements( obj )             n = obj.nextInsert - obj.nextRemove;         end     end end 
like image 30
Edric Avatar answered Oct 01 '22 16:10

Edric