3

I am following this answer, to implement a queue in matlab.

But unlike in the answer I am not enqueueing strings but rather structs. Here is my sample code

import java.util.LinkedList;
queue = LinkedList();
queue.add(struct('f', f, 'A', A)) % f and A are matrices

But this gives me an error

No method 'add' with matching signature found for class 'java.util.LinkedList'.

How do I fix this?

Community
  • 1
  • 1
ssb
  • 7,422
  • 10
  • 36
  • 61

1 Answers1

3

MATLAB has a pretty intimate connection with Java, automatically converting many MATLAB native data types to and from their Java counterparts... except for structs. This makes sense if you think about it: there is a natural way to map POD types, and arrays of POD types, and even cell arrays map naturally to java.lang.Object arrays. But creating a Java representation of a struct would require a corresponding class definition. See this MATLAB documentation for details.

So storing a MATLAB struct in a Java container will require some additional, possibly third-party code. Instead, what are you trying to do? Depending on your requirements (i.e., how big do you expect your queue to get, etc.), it may be perfectly fine to implement your queue with a simple cell array:

>> queue = {}; % create empty queue
>> queue{end+1} = x; % push x onto queue
>> q(1) = []; % pop top element from queue

But a sequence of pushes will take quadratic time, right? Yes... but the constant on that O(n^2) is reeaalllly small, so that this implementation will beat many other non-MEXed implementations for speed.

  • I am basically trying to implement a Integer Program solver using a Linear program solver. And I tried this, and its just not fast enough for what I need. Is there any other way to solve this? – ssb Jan 23 '13 at 20:44