I have 2 matrices and an empty matrix that I want to assign the results to. When I want to print the results, I can make comparisons and get the correct results. But when I want to assign these comparisons to the empty matrix, I get an error. what should I do? Note: I want to use a for loop
mat1=matrix(c(5,4,6,8,9,7,3,5,6,2),nrow = 10, ncol = 1)
mat2=matrix(c(1,4,5,7,3),nrow = 1,ncol = 5)
mat3 = matrix(0L, nrow = 9, ncol = 5)
for (j in 1:ncol(mat2)){
for (i in 1:nrow(mat1)){
if (mat2[,j] < mat1[i,]) {
print(paste("1", i))
}
else {
print(paste("0", i))
}
}
}
enter image description here this code works smoothly and gives the correct results of the comparisons. But I cannot correctly assign the results I want in the code block below to the matrix.enter image description here as a result I need to get a 10 * 5 matrix
mat1=matrix(c(5,4,6,8,9,7,3,5,6,2),nrow = 10, ncol = 1)
mat2=matrix(c(1,4,5,7,3),nrow = 1,ncol = 5)
mat3 = matrix(0L, nrow = 10, ncol = 5)
for (j in 1:ncol(mat2)){
for (i in 1:nrow(mat1)){
if (mat2[,j] < mat1[i,]) {
mat3[i] = 1
}
else {
mat3[i] = 0
}
}
}
Can anyone help me please
Aucun commentaire:
Enregistrer un commentaire