[R] Need help to print matrix with element and position

Anas Jamshed @n@@j@m@hed1994 @end|ng |rom gm@||@com
Thu Oct 28 10:19:22 CEST 2021


I create a matrix of size 3x3 called mat_1 and then I want to iterate over
all the values one by one and print the element as well as the position in
the matrix:

My code is :

mat_1= matrix( c('1','2','3','4','5','6','7','8','9'), nrow = 3, ncol =
3,byrow = TRUE)
mat_1
# Loop over my_matrix
for(row in 1:nrow(mat_1)) {
    for(col in 1:ncol(mat_1)) {
        print(mat_1[row, col])
    }
}

But I don't know how to print elements and positions as well of this matrix?

	[[alternative HTML version deleted]]



More information about the R-help mailing list