You were almost there. you just had to specify n
and return the right object at the end.
matrix_a = function(n) {
A = matrix(data = 0, nrow = n, ncol = n)
for (i in 1:n) {
for (j in 1:n) {
if (i>=j) {A[i,j] = (i^2)}
if (i<j) {A[i,j] = (j^2)}
}
}
A
}
matrix_a(3)
# [,1] [,2] [,3]
# [1,] 1 4 9
# [2,] 4 4 9
# [3,] 9 9 9
0
solved Function returns quadratic matrix in R [closed]