# Using a table as a variable in a function

I have a bunch of arrays and need to extract specific rows and columns of these that obey certain criteria. So for one table I'd do like this

func[x_]=Cases[table,{n_,__}/;a[x]<n<b[x]]


where b[x] and a[x] are numeric functions of a real number that I defined earlier. How can I do this so that func takes also a table as an argument? I've tried

func[table_?MatrixQ,x_]
func[table_?(MatrixQ[#,NumericQ]&),x_]


but they give me the same error

Array::argbu : Array called with 1 argument; between 2 and 4 arguments are expected


If it helps, the tables have only 2 columns (but the number of rows varies)

• you need to be using SetDelayed .. := (otherwise your pattern syntax looks correct) – george2079 Nov 24 '15 at 14:38
• Sorry, that was a typo. It is n_ in the program, I somehow deleted it when pasting it here – bernie Nov 24 '15 at 15:44
• Please post minimal, but full, version of code that gives such error. Shown error is reported by Array function, which is not used in posted code. – jkuczm Nov 26 '15 at 1:05

I believe you've just made a syntax error by leaving out && between the two inequality operators. If I understand your question, you should have functions defined as follows:

ClearAll[a, b, func];
a[x_Real] :=(* example calc *)-1*x;
b[x_Real] :=(* example calc *)x;
func[table_?(MatrixQ[#, NumericQ] &), x_] :=
Cases[table, {n_, __} /; a[x] < n && b[x] > n];


...with an example in this case of a simple filtering operation on a matrix shown as follows:

First, let's create some data. This creates a real-valued matrix:

temp = Table[RandomReal[{-1, 1}], {i, 1, 5}, {j, 1, 5}];


To work with the same data, here's what I see:

temp = {{-0.19773, -0.403283, -0.203482,
0.560051, -0.662339}, {-0.229652, -0.382093, -0.369309, 0.576484,
0.566491}, {0.30776, 0.471342, 0.556337, 0.997326,
0.749479}, {0.951397, 0.256563,
0.740539, -0.9422, -0.552236}, {0.937068, 0.903586, -0.0219966,
0.917632, 0.0494045}};


Now call func on the temp variable with some real value in place of x as follows:

func[temp, .3]


...which returns

{{-0.19773, -0.403283, -0.203482,
0.560051, -0.662339}, {-0.229652, -0.382093, -0.369309, 0.576484,
0.566491}}


You can see that func "filtered" the rows using the functions a and b (I'm just using your function names here for comparability sake) as well as the pattern variable n.

• Thanks, this seems to have worked. Just one question though: why does using my syntax works when the table is not a variable? – bernie Nov 26 '15 at 12:16
• @bernie, in the documentation for Less, for example, it indicates that the function does some simplification when its arguments are not numbers. That could be the cause. Are you sure you used SetDelayed so that your function is exactly: func[table_?(MatrixQ[#, NumericQ] &), x_] := Cases[table, {n_, __} /; a[x] < n < b[x]]? I was able to get this syntax to work actually so you weren't far off if at all. – rfrasier Nov 27 '15 at 1:29
• The problem was the SetDelayed part, that I was forgetting. Thanks for the help! – bernie Dec 1 '15 at 14:34