1function list = SJgrep (expression, index)
2%SJGREP search for matrices in the SJSU Singular Matrix Collection.
3% SJgrep returns a list of Problem id's whose Problem.name string matches an
4% expression.  With no output arguments, the list is displayed.  Otherwise, it
5% is returned as a list of integer id's.
6%
7% Example:
8%   SJgrep ('HB') ;         % all matrices in the HB group
9%   SJgrep ('\<B*') ;       % all matrices in groups starting with the letter B
10%   SJgrep ('bp') ;         % all bp matrices (in the HB group)
11%   SJgrep ('[0-9]') ;      % all matrices with a digit in their name
12%   SJgrep ('c-') ;         % all c-* optimization matrices (in 2 groups)
13%
14% An optional 2nd input argument is the SJ index, which is a little faster if
15% SJgrep is used multiple times.  With no input arguments, all Problems are
16% listed.
17%
18%   index = SJget ;
19%   SJgrep ('HB/*', index) ;
20%
21% See also regexp, SJget.
22
23%   Derived from the ssget toolbox on March 18, 2008.
24%   Copyright 2007, Timothy A. Davis
25
26if (nargin < 2)
27    index = SJget ;
28end
29
30if (nargin < 1)
31    expression = '.' ;
32end
33
34nmat = length (index.nrows) ;
35list1 = zeros (1, nmat) ;
36matched = 0 ;
37
38for id = 1:nmat
39    name = [index.Group{id} '/' index.Name{id}] ;
40    if (~isempty (regexp (name, expression, 'once')))
41	matched = matched + 1 ;
42	list1 (matched) = id ;
43    end
44end
45
46list1 = list1 (1,1:matched) ;
47
48if (nargout == 0)
49    for id = list1
50	fprintf ('%4d: %s/%s\n', id, index.Group {id}, index.Name {id}) ;
51    end
52else
53    list = list1 ;
54end
55