I need a min-heap in Matlab and I'm trying to use Java's PriorityQueue. I'm stuck on how to supply the Comparator. So far I have initialized the PriorityQueue and can add one value-index pair to it:
>> q = java.util.PriorityQueue
q =
[]
>> q.add({1,3})
ans =
1
The problem occurs when I try to add more data:
>> q.add({2,4})
??? Java exception occurred:
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to java.lang.Comparable
at java.util.PriorityQueue.siftUpComparable(Unknown Source)
at java.util.PriorityQueue.siftUp(Unknown Source)
at java.util.PriorityQueue.offer(Unknown Source)
at java.util.PriorityQueue.add(Unknown Source)
From this post, I see that I need to supply a Comparator function, but I don't know how to do this.