Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement priority queue in matlab

Tags:

matlab

Is there any library in matlab that provides the functionality of min priorityqueue

import java.util.PriorityQueue;
import java.util.*;

public class MyQueue {
  Comparator<Double> c;
  PriorityQueue<Double> PQ;

  public MyQueue() {
    c = new Comparator<Double>(){
            public int compare(Double o1, Double o2){
              if(o2 > o1) {
                return -1;
              } else if(o1 > o2) {
                return 1;
              } else {
                return 0;
              }
            }
        };
    PQ = new PriorityQueue<Double>(1000,c);
  }

  public void addElement(double d) {
    PQ.add(d);
  }

  public double removeElement() {
    return(PQ.remove());
  }
}

I have implemented this priorty queue in java. I can call it from matlab. However, I need to associate each cost with an index. I mean it's not only cost of the node that i need to store but also its index. How can I accomplish this. I need to pass the index from matlab

like image 272
user34790 Avatar asked Jan 15 '23 13:01

user34790


1 Answers

You can use Java's default PriorityQueue like so:

>> q=java.util.PriorityQueue;
>> q.add({value,index});

This is available since Java ≥ 1.5, which is pre-bundled in all MATLAB releases since 7.0.4 (R14).

Otherwise, you can use the one from the file exchange, which you'll have to compile.

There's also a Simulink block for it, but I doubt that's what you're after.

like image 133
Rody Oldenhuis Avatar answered Jan 25 '23 06:01

Rody Oldenhuis