Try this:
> x <- matrix(1:40,ncol=5)
> # function to find zero rows
> f.zero <-
+ function(arr)
+ {
+ which(apply(arr, 1, function(z) all(z == 0)))[1] # first one
+ }
>
> # now the non-zero rows
> f.nonzero <-
+ function(arr)
+ {
+ which(apply(arr, 1, function(z) any(z != 0)))
+ }
>
> f.z
Dear all,
I have a task to find the first all zero row of a matrix X ( nothing known
about X). I need to write a function which returns either the row index of
the first all-zero row, or NA if there are no all-zero rows. and I also need
to locate all rows which are non-zero (should be a vector of
2 matches
Mail list logo