immac
immac

Reputation: 17

Creating objects based on a specific restriction

How can I create the matrix A and the vector B without using the function c() and without manually inputting the values?

A .  |  1 0 0  |
     |  0 2 0  |
     |  4 0 3  |

B= 6 7 8 5 6 7 4 5 6 3 4 5 

Upvotes: 1

Views: 57

Answers (1)

Rich Scriven
Rich Scriven

Reputation: 99331

Here are my ideas:

A:

replace(diag(1:3), 3, 4)
#      [,1] [,2] [,3]
# [1,]    1    0    0
# [2,]    0    2    0
# [3,]    4    0    3

B:

rep(6:8, 4) - rep(0:3, each=3)
# [1] 6 7 8 5 6 7 4 5 6 3 4 5

Upvotes: 3

Related Questions