2

enter image description here enter image description here

Hello, I am entirely new to using R and experiencing some problems trying to develop the attached equation. Provided below is the general idea of what I am trying to code where PMU1 = omega and PMU2 = omega' from the images.

I am running into two problems that I see which is that Vh[i] is out of bounds for "i+1" when i = 7, and that I can't get a vector solution. The answer for evaluating the above omega matrix is Vh[i] = (0.25,0.25,0,0,0.5,0). I'll eventually be using a different matrix set, but I am just trying to generate a code from the equation.

PMU1 <- as.matrix(PMU1)
PMU2 <- as.matrix(PMU2)

m <- nrow(PMU1)
n <- ncol(PMU1)

for (j in 1:n)
  {
 Vh[i] <- sum(abs(PMU1[i,j]-PMU1[i+1,j]))
  }

Vh[i]
3
  • 2
    Please improve your question by more specifically describing what you are trying to achieve. Also share the code you have tried so far. Commented Mar 5, 2022 at 19:18
  • This isn't a homework question. Commented Mar 5, 2022 at 19:34
  • 1
    Your edit helps, but it still isn't a minimal reproducible example. See How to make a great R reproducible example? for some helpful tips in making a reproducible example. Commented Mar 5, 2022 at 19:51

1 Answer 1

3

While a more vectorized approach probably exists, a simple approach is to use sapply:

PMU <- matrix(c(0,1,1,1,1,1,1,0,0,1,1,1,1,1,0,0,0,0,0,1,1,0,0,0,0,0,1,1),nrow = 7)
V <- sapply(1:(nrow(PMU)-1),function(i)mean(PMU[i+1,]-PMU[i,]))

After running this code, V = 0.25 0.25 0.00 0.00 0.50 0.00

Sign up to request clarification or add additional context in comments.

3 Comments

You can also use rowMeans(diff(PMU))
@akrun. Need abs() and /n and some way to iterate across adjacent rows
@IRTFM Yes, you ar eright although it works with the example in the solution post

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.