0

This should be a fairly easy question. Under i have a matrix PX. I want to write the matrix without numerical values & with variables instead. However, when i do so i get the error code "object "p" not found.

I guess i have to undefine "p" and "q" somehow, how do you do that?

PX <- matrix(c(0, p, 0, 0, q, 0, 
           q, 0, 0, p, 0, 0,
           q, 0, 0, 0, 0, p,
           0, 0, q, 0, 0, p,
           0, 0, 0, 0, 1, 0,
           0, 0, 0, 0, 0, 1), nrow = 6, ncol = 6, byrow = TRUE)

Kind Regards,

Peter

6
  • 2
    If it a character, it needs to be quoted.
    – akrun
    Commented Jul 10, 2016 at 14:00
  • Sorry i don't undersand. do i write "p <- undefined" or something? Commented Jul 10, 2016 at 14:02
  • 1
    I meant c(0, 0, "p", 0, "q") etc.
    – akrun
    Commented Jul 10, 2016 at 14:03
  • Thank you, thats exactly what I needed. Also, is there someway to show the matrix in a nice & representable way instead of just writing "PX" Commented Jul 10, 2016 at 14:09
  • Sorry, I am not sure I understand your question.
    – akrun
    Commented Jul 10, 2016 at 14:10

1 Answer 1

2

This might help. But it won't do the final "mathy" job of putting the giant parentheses/brackets around the values in rows and columns. For that you do need LaTek or maybe MathML.

> p <- 'p'  # need a value (not necessarily "p") for the unquoted symbol - `p`
> q <- 'q'
> PX <- matrix(c(0, p, 0, 0, q, 0, 
+            q, 0, 0, p, 0, 0,
+            q, 0, 0, 0, 0, p,
+            0, 0, q, 0, 0, p,
+            0, 0, 0, 0, 1, 0,
+            0, 0, 0, 0, 0, 1), nrow = 6, ncol = 6, byrow = TRUE)
> PX
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,] "0"  "p"  "0"  "0"  "q"  "0" 
[2,] "q"  "0"  "0"  "p"  "0"  "0" 
[3,] "q"  "0"  "0"  "0"  "0"  "p" 
[4,] "0"  "0"  "q"  "0"  "0"  "p" 
[5,] "0"  "0"  "0"  "0"  "1"  "0" 
[6,] "0"  "0"  "0"  "0"  "0"  "1" 

> print(PX, quote=FALSE)
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,] 0    p    0    0    q    0   
[2,] q    0    0    p    0    0   
[3,] q    0    0    0    0    p   
[4,] 0    0    q    0    0    p   
[5,] 0    0    0    0    1    0   
[6,] 0    0    0    0    0    1   

It would also be possible to define rownames and colnames, so if you wanted to suppress the "cody" bracketed numbers, you could define all the names to be "".

> colnames(PX) <- rep("",6)
> rownames(PX) <- rep("",6)
> print(PX, quote=FALSE)

 0 p 0 0 q 0
 q 0 0 p 0 0
 q 0 0 0 0 p
 0 0 q 0 0 p
 0 0 0 0 1 0
 0 0 0 0 0 1

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.