3
Nh<-matrix(c(17,26,30,17,23, 17 ,24, 23), nrow=2, ncol=4); Nh
Sh<-matrix(c(8.290133, 6.241174, 6.096808, 7.4449672, 6.894924, 7.692115, 
             4.540521, 7.409122), nrow=2, ncol=4); Sh
NhSh<-as.matrix(Nh*Sh); NhSh
rh<-c( 0.70710678, 0.40824829, 0.28867513, 0.22360680, 0.18257419, 
       0.15430335, 0.13363062, 0.11785113, 0.10540926, 0.09534626); rh

pv <- c()
for (j in 1:2) {
  for (i in 1:4) {

    pv <- rbind(pv, NhSh[j,i]*rh)
  }
}
pv

row.names(pv) <- rep(c(1:2), each = 4)
lst<-lapply(split(seq_len(nrow(pv)), as.numeric(row.names(pv))), function(i) 
pv[i,])

data<-40
nlargest <- function(x, data) 
{
  res <- order(x)[seq_len(data)];
  pos <- arrayInd(res, dim(x), useNames = TRUE);
  list(values = pv[res], position = pos)
}
out <- lapply(lst, nlargest, data = 40)

In continuation of above code Is there any brief way of repeating the following steps for each out$’k’$position for k in 1:2?

s1<-c(1,1,1,1); ch<-c(5,7,10,5); C<-150; a<-out$'1'$position 
for (j in a[40:1, "row"] ) 
{
 s1[j] <- s1[j]+1;
 cost1 <- sum(ch*s1);
 if (cost1>=C) break
}
s1; cost1
#Output [1] 5 6 6 5  

#       [1] 152

I have to get 2 values for 's' and 'cost' for out$k$position. I tried

mat = replicate (2,{x = matrix(data = rep(NA, 80), ncol = 2)}); mat
for (k in 1:2)
{
  mat[,,k]<-out$'k'$position
}
mat

Error in mat[, , k] <- out$k$position :number of items to replace is not a multiple of replacement length

for (k in 1:2)
{
for (j in mat[,,k][40:1] ) {
  s[j] <- s[j]+1  
  cost <- sum(ch*s)
  if (cost>=C) break

}
}
s; cost

Error : Error in s[j] <- s[j] + 1 : NAs are not allowed in subscripted assignments

Please anyone help in resolving these errors.

Sam
  • 95
  • 10
  • I guess the 2nd last code is fixed and you want the last block of code to be fixed right? – akrun May 16 '18 at 07:06
  • I want it to return value of s and cost separately for mat[,,1] and mat[,,2] – Sam May 16 '18 at 07:09
  • Where did you define `s`? Is it the same `s1`? – akrun May 16 '18 at 07:09
  • Sorry, it's my mistake. It is s1 defined as `s1<-c(1,1,1,1)` – Sam May 16 '18 at 07:10
  • Also, what is `C` in the comparison? – akrun May 16 '18 at 07:13
  • `C<-150` defined in code – Sam May 16 '18 at 07:14
  • Can you check the updated solution – akrun May 16 '18 at 07:19
  • I got it. yes, that's perfectly fine. Is it possible to see the value of s for which cost remains less than 150? The result is showing those values of s for which cost exceeds 150. When i write 'n<-print(s1)' after the break condition it returns the desired output but for loop k i don't know – Sam May 16 '18 at 07:30
  • Probably, then the logic needs to be changed – akrun May 16 '18 at 07:37
  • I tried for `if (cost<=C) break ` but then it returns the initial first value of s1 for which cost was less than C and did not consider other values of s1 for which cost remained less than C – Sam May 16 '18 at 07:50

1 Answers1

2

We could apply the function directly by looping over the list. Note that each element of the list is a matrix

sapply(lst, is.matrix)
#  1    2 
#TRUE TRUE 

so, there is no need to unlist and create a matrix

out <- lapply(lst, nlargest, data = 40)

-checking with the OP's results

out1 <- nlargest(sub1, 40)
identical(out[[1]], out1)
#[1] TRUE

Update2

Based on the second update, we need to initialize 'cost' and 'sl' with the same length as 'k' elements. Here, we initialize 'sl' as a list of vectors

sl <- rep(list(c(1, 1, 1, 1)), 2)
C <- 150
cost <- numeric(2)
for (k in 1:2){

for (j in mat[,,k][40:1, 1] ) {
    sl[[k]][j] <- sl[[k]][j]+1  
    cost[k] <- sum(ch*sl[[k]])
    if (cost[k] >=C) break


    }
   }   


sl
#[[1]]
#[1] 5 7 6 4

#[[2]]
#[1] 6 5 5 7

cost
#[1] 154 150
akrun
  • 874,273
  • 37
  • 540
  • 662
  • 1
    The problem is solved. I truly appreciate your help @akrun – Sam May 14 '18 at 18:28
  • @akrun- Sorry, for the inconvenience but I need to ask In continuation of above code is there any brief way of repeating the following steps for each out$’k’$position for k in 1:100 ? `s1<-c(1,1,1,1); ch<-c(5,7,10,5); a<-out$1$position; for (j in a[40:1, "row"] ) { s1[j] <- s1[j]+1; cost1 <- sum(ch*s1); if (cost1>=C) break } s1; cost1`I have to get 100 values for 's' and 'cost' for out$k$position. – Sam May 15 '18 at 09:22
  • @Sam Could you please update your quesiton. It is difficult to read from the comments – akrun May 15 '18 at 14:20