Is there a way to use the diag()
function in a Matrix without using the built-in function or iteration?
M<-matrix(1:9, ncol=3) # make a matrix
q5b<-function(M){ #function
}
I know that M[1,1]
, M[2,2]
, and M[3,3]
will give me the same output as diag(M)
. However, I can't think of a way to do this without a for loop.
My thought process was I should have a condition where row index == column index
in the Matrix then print that value. I appreciate any suggestions.