[英]R: create diagonal from 1 to 10 in matrix -> returns only last value
我需要修改一個填充有隨機數的矩陣,以便生成的矩陣有一個數字為 1 到 10 的對角線,其他任何地方都應該是零。 我快完成了,但對角線只顯示最后一個值 10 而不是數字 1 到 10。我知道我需要以某種方式緩存結果,但我不知道該怎么做。
rand_mat = matrix(sample(1:50, 100, replace = TRUE), nrow=10, ncol=10)
#10x10 matrix filled with random numbers
for (i in 1:nrow(rand_mat)) {
#for each row
for (j in 1:ncol(rand_mat)) {
#for each column
for (o in 1:10){
#go through numbers 1 to 10 for the diagonal
if(i == j){
#if row matches column -> diagonal
rand_mat[i,j] = o
#assign numbers 1 to 10 in the diagonal
}else{
rand_mat[i,j] = 0
#if location is not in the diagonal assign 0
}
}
}
}
這是當前的結果:
> print(rand_mat)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 10 0 0 0 0 0 0 0 0 0
[2,] 0 10 0 0 0 0 0 0 0 0
[3,] 0 0 10 0 0 0 0 0 0 0
[4,] 0 0 0 10 0 0 0 0 0 0
[5,] 0 0 0 0 10 0 0 0 0 0
[6,] 0 0 0 0 0 10 0 0 0 0
[7,] 0 0 0 0 0 0 10 0 0 0
[8,] 0 0 0 0 0 0 0 10 0 0
[9,] 0 0 0 0 0 0 0 0 10 0
[10,] 0 0 0 0 0 0 0 0 0 10
>
僅使用您在問題中使用的功能:
rand_mat = matrix(sample(1:50, 100, replace = TRUE), nrow=10, ncol=10)
rand_mat <- matrix(0, 10, 10) # Make all the elements 0.
for(i in 1:10) rand_mat[i, i] <- i # Replace the diagonal elements
你最里面的循環是不必要的。 當您遍歷j
時,您將遍歷i
行中的每個單元格。 因此,如果i == j
則只需將值i
分配給單元格,否則將0
分配給單元格。 您根本不需要變量o
。
因此,您的代碼的固定版本可能是:
rand_mat = matrix(sample(1:50, 100, replace = TRUE), nrow=10, ncol=10)
for (i in 1:nrow(rand_mat)) {
#for each row
for (j in 1:ncol(rand_mat)) {
#for each column
if(i == j) {
#if row matches column -> diagonal
rand_mat[i,j] <- i
#assign row number in the diagonal
} else {
rand_mat[i,j] = 0
#if location is not in the diagonal assign 0
}
}
}
rand_mat
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#> [1,] 1 0 0 0 0 0 0 0 0 0
#> [2,] 0 2 0 0 0 0 0 0 0 0
#> [3,] 0 0 3 0 0 0 0 0 0 0
#> [4,] 0 0 0 4 0 0 0 0 0 0
#> [5,] 0 0 0 0 5 0 0 0 0 0
#> [6,] 0 0 0 0 0 6 0 0 0 0
#> [7,] 0 0 0 0 0 0 7 0 0 0
#> [8,] 0 0 0 0 0 0 0 8 0 0
#> [9,] 0 0 0 0 0 0 0 0 9 0
#> [10,] 0 0 0 0 0 0 0 0 0 10
或者,更簡潔地說:
for (i in 1:nrow(rand_mat))
for (j in 1:ncol(rand_mat))
rand_mat[i, j] <- if(i == j) i else 0
由reprex 包於 2022-05-14 創建 (v2.0.1)
感謝@dcarlson,我得到了它他們的代碼肯定要短得多,但我真的很想用我的代碼來看看我做錯了什么。 原來我不得不在 if-else-loop 中寫rand_mat[o,o] = o
而不是rand_mat[i,j] = o
。 所以代碼現在看起來像這樣:
rand_mat = matrix(sample(1:50, 100, replace = TRUE), nrow=10, ncol=10)
#10x10 matrix filled with random numbers
for (i in 1:nrow(rand_mat)) {
#for each row
for (j in 1:ncol(rand_mat)) {
#for each column
for (o in 1:10){
#go through numbers 1 to 10 for the diagonal
if(i == j){
#if row matches column -> diagonal
rand_mat[o,o] = o
#assign numbers 1 to 10 in the diagonal
}else{
rand_mat[i,j] = 0
#if location is not in the diagonal assign 0
}
}
}
}
當我使用@dcarlson 的代碼嘗試它時,結果看起來像
> print(rand_mat)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 1 0 0 0 0 0 0 0 0 0
[2,] 0 2 0 0 0 0 0 0 0 0
[3,] 0 0 3 0 0 0 0 0 0 0
[4,] 0 0 0 4 0 0 0 0 0 0
[5,] 0 0 0 0 5 0 0 0 0 0
[6,] 0 0 0 0 0 6 0 0 0 0
[7,] 0 0 0 0 0 0 7 0 0 0
[8,] 0 0 0 0 0 0 0 8 0 0
[9,] 0 0 0 0 0 0 0 0 9 0
[10,] 0 0 0 0 0 0 0 0 0 10
>
但正如我從@Allan Cameron 那里了解到的,你不需要變量o
所以你甚至不需要第三個 for 循環:
rand_mat = matrix(sample(1:50, 100, replace = TRUE), nrow=10, ncol=10)
for (i in 1:nrow(rand_mat)) {
#for each row
for (j in 1:ncol(rand_mat)) {
#for each column
if(i == j) {
#if row matches column -> diagonal
rand_mat[i,j] <- i
#assign row number in the diagonal
} else {
rand_mat[i,j] = 0
#if location is not in the diagonal assign 0
}
}
}
結果是一樣的。 謝謝您的幫助!
盡管這不是執行此操作的有效方法(如注釋中所述),但您可以通過刪除最內層循環並將其替換為賦值來修復代碼:
for (i in 1:nrow(rand_mat)) {
for (j in 1:ncol(rand_mat)) {
rand_mat[i,j] = if(i == j) i else 0
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.