难以在for循环中维护x [i,i]

时间:2018-05-31 11:08:29

标签: r

我一直遇到问题

sum <- (abs(x[i, i] - x[i, j])^p) ^ (1/p) 

我希望它是例如x[1,1]x[2,2]等等。所以我认为for循环中的x[i,i]会完成这项工作,但它只会让我回到0

example <- function (x,p) {

sum <- 0

for (i in 1:ncol(x)) {

  for (j in i:nrow(x)){

    sum<-(abs(x[i,i] - x[i,j])^p) ^ (1/p)

  }

}

  return (sum)

}

 #x is a matrix

1 个答案:

答案 0 :(得分:2)

每次循环时都要替换总和, // EventJob runs once every 10 seconds class EventJob extends Actor { val EventListnerPoolOfActors = ActorSystem().actorOf( RoundRobinPool(10) .props(Props(classOf[EventHandler])), "InjectorActorID" ) override def preStart(): Unit = { self ! ReceivedJobStart() } def receive: Actor.Receive = { case ReceivedJobStart() => doWork() context.system.scheduler.scheduleOnce(10, self, ReceivedJobStart()) } def doWork(): Future[Unit] = { // returns Future[Seq[Event]] getUnprocessEvents().map { x => { // pass each Event to an EventHandler Actor to process for (a <- 0 to x.size) { EventListnerPoolOfActors ! x(a) } } } } } class EventHandler extends Actor { def receive = { ... } } 应该这样做。