Попытка напечатать позиции из матрицы и поставить запятые между ними - PullRequest
0 голосов
/ 14 октября 2019

Я пытаюсь напечатать положения матрицы и сделать так, чтобы читатель печатного текста понял, что пара чисел соответствует одной позиции в данной матрице (строка и столбец). Тем не менее, печать выглядит примерно так для 3 позиций: 1 5 3 6 8 6

Было бы неплохо напечатать это так: [1,5], [3, 6] и т. Д.

#Creating matrix 1
matrix1 <- matrix(data = 1:16, ncol = 2, byrow = TRUE)
matrix1

#Creating a second matrix
matrix2 <- matrix(data = 2, nrow = 2, ncol = 8)
matrix2

#Matrix multiplication
matrix3 <- matrix1 %*% matrix2
matrix3

#Storing the positions of the highest values in matrix 3
position_matrix3 <- which(matrix3 == max(matrix3), arr.ind = TRUE)

#Since there are several "62"s in matrix 3, I tell R to only take the first value from position_matrix3
value_matrix3 <- matrix3[position_matrix3[1,1]]

#Here I want to print the one value (62) and show all the positions of 62 in the following [8, 8].... etc.

cat("The position of the maximum value in matrix3 are", position_matrix3, "and the corresponding value is", 
    value_matrix3)

Вывод:

The position of the maximum value in matrix3 are 8 8 8 8 8 8 8 8 1 2 3 4 5 6 7 8 and the corresponding value is 62

1 Ответ

1 голос
/ 14 октября 2019

Здесь

cat("The position of the maximum value in matrix3 are", value_matrix3, "and the corresponding value is", 
    paste0("[",position_matrix3[,1],",",position_matrix3[,2],"]")
)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...