Home > Blockchain >  (R) Given a matrix, how to find which row has the most elements which is less than following element
(R) Given a matrix, how to find which row has the most elements which is less than following element

Time:10-28

Given a matrix, how to find which row has the most elements which is less than following elements to the right? (without for loop or apply)

I am trying to do this on matrix ( seq(1,1000) , 1000, 1000), but i am getting 22 seconds

> A =
> 6 4 2
> 3 3 3
> 1 2 3
> 7 8 8
> 2 4 5

For this matrix, we would pick row 3 and row 5

CodePudding user response:

What about this?

maxi <- t(apply(m, 1, function(x) sum(diff(x) > 0)))
which(maxi == max(maxi))
  • Related