ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 22 Apr 2020 23:00:16 +0200Putting a vector into part of a row of a matrixhttps://ask.sagemath.org/question/50841/putting-a-vector-into-part-of-a-row-of-a-matrix/So from the link given here- https://ask.sagemath.org/question/8366/editing-entries-of-matrix/
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])`?Sat, 18 Apr 2020 18:04:49 +0200https://ask.sagemath.org/question/50841/putting-a-vector-into-part-of-a-row-of-a-matrix/Answer by slelievre for <p>So from the link given here- <a href="https://ask.sagemath.org/question/8366/editing-entries-of-matrix/">https://ask.sagemath.org/question/836...</a></p>
<p>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 </p>
<pre><code>K[1, :] = vector(List1)
</code></pre>
<p>For example,</p>
<pre><code>K = Matrix(QQ, 6, 8)
</code></pre>
<p>A priori, this is just the 0 matrix. Now writing</p>
<pre><code>K[1, :] = vector([1, 1, 1, 1, 1, 1, 1, 1])
</code></pre>
<p>will replace my first row of my 6 x 8 matrix with the entries all 1's.</p>
<p>However, what if I want to replace say row 1 but only the last 6 entries
so I will have <code>[0, 0, 1, 1, 1, 1, 1, 1]</code>. Of course, I can just type out</p>
<pre><code>K[1, :] = vector([0, 0, 1, 1, 1, 1, 1, 1])
</code></pre>
<p>but this is not viable for larger matrices. </p>
<p>An example would be say my <code>K = Matrix(QQ, 40, 80)</code> and I have a list given by <code>L1 = [1, 1, 1, 1]</code>.
Suppose I want to replace the row 1 with 40th-43rd entry being <code>L1</code>.</p>
<p>Method 1- Type out 76 zeros and do <code>K[1, :] = vector(L1 with 76 zeros)</code>. This is not really ideal.</p>
<p>Method 2- Replace entry by entry. This is also not ideal if my <code>L1</code> is say of length 20.</p>
<p>Is there a way to just say something like <code>K[1, 40-43] = vector([L1])</code>?</p>
https://ask.sagemath.org/question/50841/putting-a-vector-into-part-of-a-row-of-a-matrix/?answer=50842#post-id-50842To 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)
Sat, 18 Apr 2020 18:34:30 +0200https://ask.sagemath.org/question/50841/putting-a-vector-into-part-of-a-row-of-a-matrix/?answer=50842#post-id-50842Answer by Juanjo for <p>So from the link given here- <a href="https://ask.sagemath.org/question/8366/editing-entries-of-matrix/">https://ask.sagemath.org/question/836...</a></p>
<p>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 </p>
<pre><code>K[1, :] = vector(List1)
</code></pre>
<p>For example,</p>
<pre><code>K = Matrix(QQ, 6, 8)
</code></pre>
<p>A priori, this is just the 0 matrix. Now writing</p>
<pre><code>K[1, :] = vector([1, 1, 1, 1, 1, 1, 1, 1])
</code></pre>
<p>will replace my first row of my 6 x 8 matrix with the entries all 1's.</p>
<p>However, what if I want to replace say row 1 but only the last 6 entries
so I will have <code>[0, 0, 1, 1, 1, 1, 1, 1]</code>. Of course, I can just type out</p>
<pre><code>K[1, :] = vector([0, 0, 1, 1, 1, 1, 1, 1])
</code></pre>
<p>but this is not viable for larger matrices. </p>
<p>An example would be say my <code>K = Matrix(QQ, 40, 80)</code> and I have a list given by <code>L1 = [1, 1, 1, 1]</code>.
Suppose I want to replace the row 1 with 40th-43rd entry being <code>L1</code>.</p>
<p>Method 1- Type out 76 zeros and do <code>K[1, :] = vector(L1 with 76 zeros)</code>. This is not really ideal.</p>
<p>Method 2- Replace entry by entry. This is also not ideal if my <code>L1</code> is say of length 20.</p>
<p>Is there a way to just say something like <code>K[1, 40-43] = vector([L1])</code>?</p>
https://ask.sagemath.org/question/50841/putting-a-vector-into-part-of-a-row-of-a-matrix/?answer=50845#post-id-50845@ 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*.Sat, 18 Apr 2020 18:59:10 +0200https://ask.sagemath.org/question/50841/putting-a-vector-into-part-of-a-row-of-a-matrix/?answer=50845#post-id-50845Comment by slelievre for <p>@ whatupmatt , as an alternative, you can do</p>
<pre><code>K[1, -6:] = ones_matrix(1,6)
</code></pre>
<p>and </p>
<pre><code>K[1, 40:44] = ones_matrix(1,4)
</code></pre>
<p>A warning: remember that in Python the first row of the matrix is row 0, so <code>K[1,...]</code> modifies the <em>second row</em>.</p>
https://ask.sagemath.org/question/50841/putting-a-vector-into-part-of-a-row-of-a-matrix/?comment=50863#post-id-50863Turned 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.Sun, 19 Apr 2020 16:12:11 +0200https://ask.sagemath.org/question/50841/putting-a-vector-into-part-of-a-row-of-a-matrix/?comment=50863#post-id-50863Comment by whatupmatt for <p>@ whatupmatt , as an alternative, you can do</p>
<pre><code>K[1, -6:] = ones_matrix(1,6)
</code></pre>
<p>and </p>
<pre><code>K[1, 40:44] = ones_matrix(1,4)
</code></pre>
<p>A warning: remember that in Python the first row of the matrix is row 0, so <code>K[1,...]</code> modifies the <em>second row</em>.</p>
https://ask.sagemath.org/question/50841/putting-a-vector-into-part-of-a-row-of-a-matrix/?comment=50865#post-id-50865Great, thanks a lot.Sun, 19 Apr 2020 17:54:27 +0200https://ask.sagemath.org/question/50841/putting-a-vector-into-part-of-a-row-of-a-matrix/?comment=50865#post-id-50865Comment by slelievre for <p>@ whatupmatt , as an alternative, you can do</p>
<pre><code>K[1, -6:] = ones_matrix(1,6)
</code></pre>
<p>and </p>
<pre><code>K[1, 40:44] = ones_matrix(1,4)
</code></pre>
<p>A warning: remember that in Python the first row of the matrix is row 0, so <code>K[1,...]</code> modifies the <em>second row</em>.</p>
https://ask.sagemath.org/question/50841/putting-a-vector-into-part-of-a-row-of-a-matrix/?comment=50930#post-id-50930@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.Wed, 22 Apr 2020 23:00:16 +0200https://ask.sagemath.org/question/50841/putting-a-vector-into-part-of-a-row-of-a-matrix/?comment=50930#post-id-50930