Fill an empty matrix in R
In this article, we will discuss how to fill the empty matrix with values in R Programming Language. First, let’s create an empty matrix.
Syntax:
matrix_name[row,column]=value.
Where, row and column are the numbers in which the value is occupied.
Example:
R
a<- matrix (nrow=4,ncol=4)
print (a)
a[2,4]=4
print (a)
a<- matrix (nrow=4,ncol=4)
a[,4]=4
print (a)
a<- matrix (nrow=4,ncol=4)
a[4,]=4
print (a)
|
Output:
We can replace with same value for all empty value in a matrix, by using [,].
Example
R
a<- matrix (nrow=4,ncol=4)
print (a)
a[,]=5
print (a)
|
Output:
Even a range can be inserted.
Example
R
a<- matrix (nrow=4,ncol=4)
print (a)
a[,]=1:16
print (a)
|
Output:
Last Updated :
21 Apr, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...