R plot of a matrix of complex numbers

1.2k views Asked by At

I have a matrix of complex values.

If I issue the command:

plot(myMatrix)

then it shows on the graphics device a kind of scatterplot, with X-axis labeled Re(myMatrix) and Y-axis with Im(myMatrix). This shows the information I'm looking for, as I can see distinct clusters, that I cannot see with only one column.

My questions are :

  1. I assume there is one point per matrix row. Is it right ?
  2. How is calculated Re(myMatrix) for each row vector ?

It is not Re(myMatrix[1,row]), but seems to be a mix of all values of row vector. I would like to be able to get these values, so to know how to compute them with R.

1

There are 1 answers

2
Roland On BEST ANSWER

No, there is one point for each matrix element.

set.seed(42)
mat <- matrix(complex(real = rnorm(16), imaginary = rlnorm(16)), 4) 
plot(mat)

points(Re(mat[1,1]), Im(mat[1,1]), col = "red", pch = ".", cex = 5)

Look for the red dot: resulting plot

You'd get the same plot, if you plotted a vector instead of a matrix, i.e., plot(c(mat)).

This happens because plot.default calls xy.coords and that function contains the following code:

else if (is.complex(x)) {
            y <- Im(x)
            x <- Re(x)
            xlab <- paste0("Re(", ylab, ")")
            ylab <- paste0("Im(", ylab, ")")
        }
        else if (is.matrix(x) || is.data.frame(x)) { 

This means, that the fact that input is complex takes priority over it being a matrix.