Provide avectorized solution that replaces the entire expression in these function by aone line expression that does not use loop. You can assume x is a numeric vector and valis a single numeric value.
a) val.positions<-function(x,val)
{
val.pos<-NULL
for(i in 1:length(x) )
if (x==val) val.pos<-c(val.pos,i)
val.pos
}
b) monotonic<-function(x)
{ monotonic.increasing<-TRUE
monotonic.decreasing<-TRUE
for(iin 2:length(x)) if(x-x[i-1]<0) monotonic.increasing<-FALSE
for(i in 2:length(x)) if(x-x[i-1]>0)monotonic.decreasing<-FALSE
monotonic.increasing | monotonic.decreasing
}
这道题完全没有头绪,哪位热心的前辈能帮忙解答一下或者给一些思路,万分感谢。