楼主: 风暴之灵233
1629 3

[问答] Vectorizing functions [推广有奖]

  • 0关注
  • 0粉丝

小学生

21%

还不是VIP/贵宾

-

威望
0
论坛币
4 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
33 点
帖子
2
精华
0
在线时间
8 小时
注册时间
2015-4-15
最后登录
2019-4-7

楼主
风暴之灵233 发表于 2015-4-23 16:58:29 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

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

     }

这道题完全没有头绪,哪位热心的前辈能帮忙解答一下或者给一些思路,万分感谢。


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Vectorizing Functions function Vector tions expression function solution entire single

沙发
nuomin 发表于 2015-4-23 18:00:53
意思是把自定义函数里的fro循环用函数apply()代替

藤椅
风暴之灵233 发表于 2015-4-23 20:10:38
nuomin 发表于 2015-4-23 18:00
意思是把自定义函数里的fro循环用函数apply()代替
谢谢您的提示,我已经会做了

板凳
公瑾- 发表于 2019-6-17 15:19:38
怎么做的啊,这个题我们也出了,但我不理解。。。一个四年后的回复

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-24 14:55