Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

This is the following matrix I am working with:

A = diag(5)

A

     [,1] [,2] [,3] [,4] [,5]

[1,]    1    0    0    0    0

[2,]    0    1    0    0    0

[3,]    0    0    1    0    0

[4,]    0    0    0    1    0

[5,]    0    0    0    0    1

Now I want to add 1st column of the A matric in new_A

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]

[1,]    1    1    1    1    0    0    0    0

[2,]    0    0    0    0    1    0    0    0

[3,]    0    0    0    0    0    1    0    0

[4,]    0    0    0    0    0    0    1    0

[5,]    0    0    0    0    0    0    0    1

1 Answer

0 votes
by (36.8k points)

It's very easy you just need to repeat from 1 to n time and append the other columns:

n = 3

A[, c(rep(1, n + 1), 2:ncol(A))]

#     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]

#[1,]    1    1    1    1    0    0    0    0

#[2,]    0    0    0    0    1    0    0    0

#[3,]    0    0    0    0    0    1    0    0

#[4,]    0    0    0    0    0    0    1    0

#[5,]    0    0    0    0    0    0    0    1

Improve your knowledge in data science from scratch using Data science online courses 

Browse Categories

...