Is there any data type equivalent for queue?
이전 댓글 표시
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.
답변 (3개)
Guillaume
2020년 4월 30일
2 개 추천
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.
댓글 수: 4
ROSHITH SEBASTIAN
2020년 4월 30일
Guillaume
2020년 5월 9일
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
2020년 5월 11일
편집: ROSHITH SEBASTIAN
2020년 5월 11일
Anubhav Kumar
2021년 3월 19일
@Guillaume I have attached queue.m file in the same directory where my main.m file is present and I am trying to use queue but in main.m where I have written q = queue; on runnung the script error is thrwoing at q = queue.
please help me so that I can solve my issue.
parallel.pool.PollableDataQueue
As revealed by the package name, it can be shared among parallel workers.
Mahendra
2023년 8월 9일
0 개 추천
Instead of resizing arrays during execution, you can initialize them with zeros up to a generous length. This way, you avoid frequent size adjustments and can simply track the relevant data within this pre-allocated space.
I frequently do this and found it to not have any speed penalties. We are wasting RAM but rarely are a problem on modern day PCs.
댓글 수: 1
Melih Furkan SEN
2023년 11월 3일
And how will you pop them ?) or sort?)
카테고리
도움말 센터 및 File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!