Suppose I have a list of the form x={{a,b},{n1,n2,...}
where n1,n2... are integers.
I want to write a function that checks whether each of n1,n2, etc. is greater than a limit. If that's the case x
is set to 0.
, else the function returns x
.
fc[x_List] :=Module[{i, l}, l = Length[x[[2]]];
For[i = 1, i <= l, i++, If[x[[2, i]] > smax, x = 0.; Break[]]];
x];
Let's try it:
smax = 2; x = {{1,5}, {1, 2, 3, 4}};
fc[x]
(*=> fc[0.] *)
and not 0
. What am I doing wrong in the definition of fc
? Also, is there a way to write fc
without usinf the For
loop?
EDIT
After discovering AnyTrue
thanks to the answer of @SneezeFor16Min who mentions AllTrue
the answer is
Clear[fc]
fc[x_List] := If[AnyTrue[x[[2]], # > smax &], 0., x]
Scan
can replaceFor
in this case. (2)x
in the definition offc
is aList
, not aSymbol
, sox = 0.
is equivalent to{{1,5}, {1, 2, 3, 4}} = 0.
which won't work. You should use temporary variable for return. $\endgroup$GreaterThan[smax]
(not a big deal). $\endgroup$