ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 09 Aug 2019 01:43:20 -0500removing the repeated rows of a matrixhttp://ask.sagemath.org/question/47418/removing-the-repeated-rows-of-a-matrix/ My sage program outputs a matrix with integer entries in which some rows are repeated. But I want to remove the multiple entries of rows in the output. Kindly somebody explains to me how to do this.
Thank you.Wed, 07 Aug 2019 06:54:59 -0500http://ask.sagemath.org/question/47418/removing-the-repeated-rows-of-a-matrix/Answer by dsejas for <p>My sage program outputs a matrix with integer entries in which some rows are repeated. But I want to remove the multiple entries of rows in the output. Kindly somebody explains to me how to do this.</p>
<p>Thank you.</p>
http://ask.sagemath.org/question/47418/removing-the-repeated-rows-of-a-matrix/?answer=47430#post-id-47430Hello, @GA316. There are two ways to do this, depending on whether you want to do it manually or automatically.
To remove rows manually, you just need to use the `delete_rows` method: Suppose you have a matrix `A` such that its 3rd, 5th and 6th rows are exactly the same as the first row, so they should be removed. Just execute:
A.delete_rows([3, 5, 6])
If you want to do it automatically, the following piece of code can help you:
def remove_repeated_rows(A):
nr = A.nrows()# number of rows
eliminate = []# this will store the numbers of the rows to eliminate
for i in range (nr-1):# check all rows, except the last one
for j in range(i+1, nr):# compare to the rows below row i
if A[i] == A[j]: eliminate.append(j)# if row j equals row i, list j to eliminate
return A.delete_rows(eliminate)# eliminate the listed rows and return the resulting submatrix
I hope this helps!Wed, 07 Aug 2019 23:14:31 -0500http://ask.sagemath.org/question/47418/removing-the-repeated-rows-of-a-matrix/?answer=47430#post-id-47430Comment by dsejas for <p>Hello, <a href="/users/8777/ga316/">@GA316</a>. There are two ways to do this, depending on whether you want to do it manually or automatically.</p>
<p>To remove rows manually, you just need to use the <code>delete_rows</code> method: Suppose you have a matrix <code>A</code> such that its 3rd, 5th and 6th rows are exactly the same as the first row, so they should be removed. Just execute:</p>
<pre><code>A.delete_rows([3, 5, 6])
</code></pre>
<p>If you want to do it automatically, the following piece of code can help you:</p>
<pre><code>def remove_repeated_rows(A):
nr = A.nrows()# number of rows
eliminate = []# this will store the numbers of the rows to eliminate
for i in range (nr-1):# check all rows, except the last one
for j in range(i+1, nr):# compare to the rows below row i
if A[i] == A[j]: eliminate.append(j)# if row j equals row i, list j to eliminate
return A.delete_rows(eliminate)# eliminate the listed rows and return the resulting submatrix
</code></pre>
<p>I hope this helps!</p>
http://ask.sagemath.org/question/47418/removing-the-repeated-rows-of-a-matrix/?comment=47444#post-id-47444Happy to help!Fri, 09 Aug 2019 01:43:20 -0500http://ask.sagemath.org/question/47418/removing-the-repeated-rows-of-a-matrix/?comment=47444#post-id-47444Comment by GA316 for <p>Hello, <a href="/users/8777/ga316/">@GA316</a>. There are two ways to do this, depending on whether you want to do it manually or automatically.</p>
<p>To remove rows manually, you just need to use the <code>delete_rows</code> method: Suppose you have a matrix <code>A</code> such that its 3rd, 5th and 6th rows are exactly the same as the first row, so they should be removed. Just execute:</p>
<pre><code>A.delete_rows([3, 5, 6])
</code></pre>
<p>If you want to do it automatically, the following piece of code can help you:</p>
<pre><code>def remove_repeated_rows(A):
nr = A.nrows()# number of rows
eliminate = []# this will store the numbers of the rows to eliminate
for i in range (nr-1):# check all rows, except the last one
for j in range(i+1, nr):# compare to the rows below row i
if A[i] == A[j]: eliminate.append(j)# if row j equals row i, list j to eliminate
return A.delete_rows(eliminate)# eliminate the listed rows and return the resulting submatrix
</code></pre>
<p>I hope this helps!</p>
http://ask.sagemath.org/question/47418/removing-the-repeated-rows-of-a-matrix/?comment=47432#post-id-47432This is a great answer and it works perfectly. Thanks for your valuable time.Thu, 08 Aug 2019 01:11:29 -0500http://ask.sagemath.org/question/47418/removing-the-repeated-rows-of-a-matrix/?comment=47432#post-id-47432