I have this matrix:
let arr = Array.make_matrix 4 4 0;;
and what to check if all elements are 0.
I heard of the function for_all but I can’t quite figure it out how to use it with a matrix, since it expects an int array or a int list.
I have this matrix:
let arr = Array.make_matrix 4 4 0;;
and what to check if all elements are 0.
I heard of the function for_all but I can’t quite figure it out how to use it with a matrix, since it expects an int array or a int list.
Beginner response here:
Array.for_all (fun x -> Array.for_all(fun y -> y = 0) x) arr;;
Array.make_matrix
creates an array of arrays whereas for_all
works in an individual array and therefore does not give you access to all (x,y)
entries in the matrix. @bbutterfield provides an answer.
If you need this test often on a fixed small dimension, a fast way would be to define a matrix and compare it directly:
let zero = Array.make_matrix 4 4 0
Now you can compare arr = zero
by taking advantage of the polymorphic comparison operator (=)
that will do the traversal internally