banner



How To Add Column To Matrix Matlab

  • Updated date Jun 22, 2020
  • 340.4k
  • half-dozen

In this article, nosotros shall learn how to add rows and column to a matrix in R using R studio?

Introduction

R is an of import programming language used heavily by statisticians. It is as well used in machine learning, data science, inquiry, and many more new fields. It is a computing environs where statistical information may be implemented.

In a previous article, nosotros had learned about matrices in R. In this article, we shall larn how to add rows, columns to a matrix.

Let's get started now.

Calculation Row To A Matrix We use function rbind() to add the row to any existing matrix. To know rbind() function in R only type ?rbind() or aid(rbind) R studio, information technology will give the result as below in the image.

  1. ?rbind()
  2. help(rbind)

R

Using rbind() part To show how to use rbind() part in R nosotros shall first create and print a matrix. After that, nosotros shall use rbind() function and so run into the output of using rbind() part by printing again the previously created matrix.

Footstep one - Creating And Printing A Matrix in R Studio

  1. MatrixA <- matrix(data = 1:9, nrow = three, ncol = 3)
  2. MatrixA

Output MatrixA

[,one] [,ii] [,iii]

[1,] 1 4 7

[2,] two 5 8

[3,] 3 six ix

R

Stride 2 - Using rbind() office

  1. MatrixB <- rbind(MatrixA, c(ten,xi,12))
  2. MatrixB

Output > MatrixB

[,one] [,2] [,iii]

[1,] ane 4 seven

[2,] two 5 8

[3,] 3 6 9

[iv,] 10 11 12

Here, a new matrix named MatrixB has been created which is the combination of a new row with values x, 11, and 12 in the previous matrix with the proper name MatrixA. It has been shown in the beneath paradigm how it looks in R Studio.

R

Stride 3 - Divergence Between Both Matrices

As we tin can see conspicuously the number of rows in MatrixA and MatrixB are different. MatrixA has 3 rows while MatrixB has 4 rows. This extra ane row has been added in MatrixA using rbind() function.

Step four - Using str() function to see the difference in both Matrices

The str() function in R gives the construction of an object. To use it we simply provide an object as its statement. Hither, nosotros shall print the structure of MatrixA and MatrixB to see the difference as below.

  1. str(MatrixA)
  2. str(MatrixB)

Output > str(MatrixA)

int [i:3, 1:3] 1 2 3 4 5 6 7 8 9

> str(MatrixB)

num [1:4, 1:3] 1 2 3 ten 4 five 6 11 seven 8 ...

R

Adding Cavalcade To A Matrix

For adding a column to a Matrix in nosotros use cbind() office. To know more about cbind() role simply type ?cbind() or help(cbind) in R. It will display documentation of cbind() function in R documentation as shown below.

  1. ?cbind()
  2. help(cbind)

R

Using cbind() part We shall use the in a higher place matrix with the created proper name MatrixA. Now we shall add a new column into it using cbind() office as below. We shall give the name of this matrix MatrixC.

  1. MatrixC <- cbind(MatrixA, c(10, 11, 12))
  2. MatrixC

Output > MatrixC

[,1] [,two] [,3] [,four]

[1,] i 4 7 10

[2,] 2 5 8 eleven

[3,] 3 6 9 12

R

Here we have added a new column i.eastward., the 4th column with data 10, 11, and 12 using cbind() function. It has been shown in the to a higher place prototype and we can also print the construction of MatrixC to know this equally below.

  1. str(MatrixC)

Output num [1:3, 1:4] ane 2 3 4 5 vi 7 eight ix ten ...

R

Matrix After using rbind() And cbind() function - Conclusion Equally higher up we created two matrices named MatrixB and MatrixC using two different functions rbind() and cbind() in R studio. At present we can encounter the structure of MatrixB and MatrixC using str() function. Both matrices have been created with MatrixA which was of the dimension 3*three i.due east., 3 rows and 3 columns. Now MatrixB has become of the dimension 4 rows and 3 columns. The number of rows became here 4 from three rows considering nosotros take used rbind() function to add rows and hence the data of columns and number of columns remains the same. MatrixC has been created by cbind() function hence the number of columns has go 4 from 3 while the number of rows remains the aforementioned. Its dimension has go iii*iv i.east., three rows and 4 columns.

  1. str(MatrixB)
  2. str(MatrixC)

Output > str(MatrixB)

num [ane:4, i:iii] ane 2 3 x iv 5 6 11 7 viii ...

> str(MatrixC)

num [i:iii, ane:iv] 1 2 three 4 5 6 7 8 9 10 ...

R

Summary

In this article, we have learned how to add row and column to a matrix in R using R studio and we saw how to print the structure of an object using str() function.

I hope you accept learned and enjoyed reading this article. You're welcome to like, comment, share and leave whatever blazon of suggestion regarding this information.

How To Add Column To Matrix Matlab,

Source: https://www.c-sharpcorner.com/article/matrix-in-r-adding-rows47columns-and-accessing-elements-by-name-in-matrix-i/

Posted by: bastarachemeself.blogspot.com

0 Response to "How To Add Column To Matrix Matlab"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel