Ask Your Question
2

Putting a vector into part of a row of a matrix

asked 2020-04-18 18:04:49 +0100

whatupmatt gravatar image

updated 2020-04-18 18:30:46 +0100

slelievre gravatar image

So from the link given here- https://ask.sagemath.org/question/836...

From the answer given, I see that to replace a row (say row 1) on a matrix by a list say List 1, I just do

K[1, :] = vector(List1)

For example,

K = Matrix(QQ, 6, 8)

A priori, this is just the 0 matrix. Now writing

K[1, :] = vector([1, 1, 1, 1, 1, 1, 1, 1])

will replace my first row of my 6 x 8 matrix with the entries all 1's.

However, what if I want to replace say row 1 but only the last 6 entries so I will have [0, 0, 1, 1, 1, 1, 1, 1]. Of course, I can just type out

K[1, :] = vector([0, 0, 1, 1, 1, 1, 1, 1])

but this is not viable for larger matrices.

An example would be say my K = Matrix(QQ, 40, 80) and I have a list given by L1 = [1, 1, 1, 1]. Suppose I want to replace the row 1 with 40th-43rd entry being L1.

Method 1- Type out 76 zeros and do K[1, :] = vector(L1 with 76 zeros). This is not really ideal.

Method 2- Replace entry by entry. This is also not ideal if my L1 is say of length 20.

Is there a way to just say something like K[1, 40-43] = vector([L1])?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
2

answered 2020-04-18 18:59:10 +0100

Juanjo gravatar image

updated 2020-04-18 18:59:22 +0100

@ whatupmatt , as an alternative, you can do

K[1, -6:] = ones_matrix(1,6)

and

K[1, 40:44] = ones_matrix(1,4)

A warning: remember that in Python the first row of the matrix is row 0, so K[1,...] modifies the second row.

edit flag offensive delete link more

Comments

Turned your comment into an answer since it's better than my answer.

In particular this can easily be adapted to replace a whole block rather than just part of a line.

slelievre gravatar imageslelievre ( 2020-04-19 16:12:11 +0100 )edit

Great, thanks a lot.

whatupmatt gravatar imagewhatupmatt ( 2020-04-19 17:54:27 +0100 )edit

@whatupmatt -- If an answer solves your question, please accept it by clicking the "accept" button (the one with a check mark, below the upvote button, score, and downvote button, at the top left of the answer).

This will mark the question as solved in the list of questions on the main page of Ask Sage, as well as in lists of questions related to a particular query or keyword.

slelievre gravatar imageslelievre ( 2020-04-22 23:00:16 +0100 )edit
0

answered 2020-04-18 18:34:30 +0100

slelievre gravatar image

To replace the last six entries of the row number 1:

K[1, -6:] = vector([1] * 6)

To replace entries 40 to 43 of row 1 with the value 1:

K[1, 40:44] = vector([1] * 4)
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2020-04-18 18:04:49 +0100

Seen: 1,423 times

Last updated: Apr 18 '20