MATLAB Answers

Is there any data type equivalent for queue?

266 views (last 30 days)
ROSHITH SEBASTIAN
ROSHITH SEBASTIAN on 30 Apr 2020
Edited: ROSHITH SEBASTIAN on 11 May 2020
Is there any data type which can handle FIFO queue operations?
I am using arrays to handle queues, but it has executional overhead since the size changes on each iteration. Now, I have to handle even array of queues, but it is getting complicated as each queue has different length at the end of each iteration. When I add an element in a row, the columns length of all the rows increases which makes it difficult to handle it.
Is there any efficient way to handle this situation. Even a link which discusses similar topics would be of great help.

  0 Comments

Sign in to comment.

Answers (1)

Guillaume
Guillaume on 30 Apr 2020
The short answer is that no, unfortunately, there's no queue implemented in base matlab (there may be something hidden in one of the toolboxes but it's unlikely you'd be able to use it generically).
I would have a look at the fileexchange, there's probably several implementations of various quality. If there's nothing good enough there, I'd look at implementing it myself. A naive implementation would just grow/shrink an array as the queue fills/empties which indeed would be very innefficient in matlab (and most languages). A better implementation would grow the array in progressively larger chunks.

  3 Comments

ROSHITH SEBASTIAN
ROSHITH SEBASTIAN on 30 Apr 2020
Thank you for the info.
I have done more research and got to know that we can import Java/C++ libraries into matlab and use their data structures. For example,"import java.util.LinkedList" will enable us to use java queue definitions in matlab. I would like to know if this is a good pratice to import libraries from java, or trying to implement a queue strucutre in matlab. If so, which is better to be imported java or C++?
Maybe, I will give more details regarding the scenario. I am creating a simulator which has many iterations which adds/deletes structures from multiple queues. It should be possible to run the simulator on every machine which has matlab installed without having to port any extra settings/libraries/dependencies/configurations.
Guillaume
Guillaume on 9 May 2020
Attached is a simple implementation of a queue in matlab. It is implemented as a class, but it should be fast.
It's not very sophisticated, the storage space of the queue will double in size every time it runs out of space and never decrease. I don't believe it would be a problem unless you have a great number of very long queues. The memory footprint of an empty queue would be |8 bytes * current capacity|.
The queue can store any kind of object, matrices, structures, etc. The type of object should have no impact on performance. Storing 5000 elements in the queue and then dequeuing them all takes around 80 millisecond on my machine.
You can create an array of queues. I'd recommend you use the static method queue.MakeVectorOfQueues unless you know how to create an array of handle objects:
%create an array of 5 queues:
myqueues = queue.MakeVectorOfQueues(5);
%fill queues at random
for i = 1:20
myqueues(randi(5)).enqueue(i);
end
%dequeue all queues:
for q = 1:5
fprintf('queue %d contained: ', q);
while myqueues(q).Depth > 0
fprintf('%d ', myqueues(q).dequeue);
end
fprintf('\n');
end
ROSHITH SEBASTIAN
ROSHITH SEBASTIAN on 11 May 2020
Thank you sharing the implementation.
I tried to realizre queues wth this approach(not array of queues, but only queues), but it consumed more time than using arrays to realise queue. The simulation duration for 3 repetitions are given below:
Using array for queue : 53.267074, 79.400834, 66.493679 seconds
Using attached file : 147.340302, 96.339909, 98.572212 seconds
I have not implemented the array of queues section of the simulator. I will try both the option and see which would be better for the simujlator as a whole.
Anyways, thanks you so, much for the effort. It might be needed in other efforts or maybe in this if it outperforms for the scenario of array of queues.
*Note : Adding 5000 elements and dequeueing them takes 0.363806 seconds in my machine, hence I think the issue is the function calls and other transfer of data using this approach

Sign in to comment.

Tags

Products


Release

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by