Since matlab does not support queues, I am using arrays to handle the queueing functionality.
I have to implement an array of queues, which each queue of different length. 2D arrays in matlab support only matrices, hence should be of same no. of elements in each queue which is not my requirement.
I imported Java implementation of linkedlist, but it does not support struct or a class object. The element to be added into a queue is a struct or a class object.
How can I handle this in the most efficient way?

 채택된 답변

John D'Errico
John D'Errico 2020년 5월 6일
편집: John D'Errico 2020년 5월 6일

0 개 추천

Standard arrays must be rectangular. However cell arrays are the perfect solution.
C = {1:3, 1:5,1:8}
C =
1×3 cell array
{1×3 double} {1×5 double} {1×8 double}
>> C{:}
ans =
1 2 3
ans =
1 2 3 4 5
ans =
1 2 3 4 5 6 7 8
You create and index them using curly braces, thus {}.
C{3}
ans =
1 2 3 4 5 6 7 8
And a cell array can contain anything at all, including structs.
Of course, an array of structs is also possible, and a struct can act as a container for anything too.

댓글 수: 6

ROSHITH SEBASTIAN
ROSHITH SEBASTIAN 2020년 5월 6일
Thank you for the answer, I think this is a good option. I will check it out!
Guillaume
Guillaume 2020년 5월 6일
I would think that using arrays as queues (whether stored in a cell array or not) would have very poor performance in matlab, at least without some clever management to avoid array resizing on each enqueue/dequeue event.
Unless, I completely misunderstood what was suggested.
ROSHITH SEBASTIAN
ROSHITH SEBASTIAN 2020년 5월 6일
Yes, it has very poor performance and consumes a lot of time. There are many insertion and deletion of elements into the queue. But, I also heard that creating classes also has a poor performance so opted against implemeting a queue by myself. Still, trying to figure out the best option.
Guillaume
Guillaume 2020년 5월 6일
I should have asked that in your previous question, can your queues have fixed (or an upper bound) size, or must they have variable size? If the former, it should be fairly easy to come up with an array-based design with very good performance.
ROSHITH SEBASTIAN
ROSHITH SEBASTIAN 2020년 5월 7일
편집: ROSHITH SEBASTIAN 2020년 5월 7일
The queues have variable length, it changes depending on the randomeness. The only known parameter before the simulation is the no. of queues, which would not be nothing less than 35, depending on the scenario could be more than even 60.
Guillaume
Guillaume 2020년 5월 9일
As a complement to my answer in your previous question, I've written you an implementation of a queue which should have good performance.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Guillaume
Guillaume 2020년 5월 6일
편집: Guillaume 2020년 5월 6일

0 개 추천

You may want to bite the bullet and implement your own queue class in matlab as I've suggested before.
Otherwise, one possibility would be to add a level of indirection to your current code. Instead of storing the objects in the queue(s), store them as values in a containers.Map and store the corresponding key in the queue. Ideally, the key would be a hash of the object but since matlab doesn't have a built-in hashing function and since containers.Map doesn't even say if it's implemented as a hashmap you could simply use continuously increasing integers. So the algorithm would go like this:
  • set up:
keycount = 0;
queueelements = containers.Map;
queue = .. %your queue implementation
  • to enqueue object s:
keycount = keycount + 1;
queueelements(keycount) = s;
queue.enqueue(keycount);
  • to dequeue object s:
key = queue.dequeue;
s = queueelements(key);
queueelements.remove(key);
edit note however that the performance of containers.Map in matlab is ... not great.

댓글 수: 1

ROSHITH SEBASTIAN
ROSHITH SEBASTIAN 2020년 5월 6일
The execution already takes a lot of time, hence I have to check which option would should be opted. Thank you the information, I will try it out.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Construct and Work with Object Arrays에 대해 자세히 알아보기

제품

릴리스

R2020a

태그

질문:

2020년 5월 6일

댓글:

2020년 5월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by