I would like to get (and store) the corresponding row and column number in a matrix, eg. to element number 12.
adj.m <- diag(c(3,2,1),nrow = 3, ncol = 3)
now i want to find the row and corresponding to element 4 or 5 or any.
adj.m[4] gives back the value(0), but I want to get and store in a variable the corresponding row and column numbers (1 and 2 in this case).
I assume its a simple command, but I could not formulate the question, that Rseek or Google returns an answer.
If
iis your index andmis your matrix, then the row and column ofican be obtained withUsage is, e.g.,
Actually, Iain’s answer is possibly more natural. You can wrap it into a function as
This also works in more than two dimensions, e.g.,