Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

dynamic datastructures in Matlab

384 views
Skip to first unread message

Yaroslav Bulatov

unread,
Aug 12, 2009, 8:57:29 PM8/12/09
to
What is a typical thing to do when you need dynamic datastructures
(like Map or Stack) in Matlab? For instance, suppose you need to
implement an algorithm that finds all independent paths in a graph
(ie, each path that is not a subset of some other path). One work-
around would be to use Java's datastructures, the drawback is that
they as well integrated into Matlab as native data types (ie, no
indexing, no arrayfun/cellfun equivalent)


import java.util.Stack;
paths=Stack();

conn=[0 1 0 0 0 0; 0 0 1 1 0 0; 0 0 0 0 1 0; 0 0 0 0 1 1; 0 0 0 0 0 0;
0 0 0 0 0 0];
% independent paths are [1,2,3,5],[1,2,4,5] and [1,2,4,6]

% initialize paths
for i=1:size(conn,1)
if isempty(find(conn(:,i),1))
path=Stack();
path.push(i);
paths.push(path);
end
end

active=paths.size();
while active>0
newpaths=Stack();
for i=1:paths.size()
path=paths.get(i-1);
last=path.peek();
next=find(conn(last,:));
active=active+numel(next)-1;
if isempty(next)
newpaths.add(path);
else
for j=1:numel(next)
current=path.clone();
current.push(next(j));
newpaths.add(current);
end
end
end
paths=newpaths;
end

0 new messages