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.Sat, 03 Dec 2016 05:58:09 -0600How to create a row that displays the counts of column entries in a matrix using python/sage?http://ask.sagemath.org/question/35493/how-to-create-a-row-that-displays-the-counts-of-column-entries-in-a-matrix-using-pythonsage/ so here is my matrix:
A = matrix([[1,1,2,2,2],
[3,1,4,3,3],
[0,4,2,4,0],
[0,0,2,0,0],
[0,0,2,0,0]])
I would like to insert a 0 row that displays the amount of row entries in each column.
This was my original code:
A = matrix([[1,1,2,2,2],
[3,1,4,3,3],
[0,4,2,4,0],
[0,0,2,0,0],
[0,0,2,0,0]])
puzzle = A.insert_row(0, sum(A))
Whats wrong with this code is that it gives me the sum of each column, when I just need the count of each column.
What I have
[ 4 6 12 9 5]
[ 1 1 2 2 2]
[ 3 1 4 3 3]
[ 0 4 2 4 0]
[ 0 0 2 0 0]
[ 0 0 2 0 0]
Desired
[ 2 3 5 3 2]
[ 1 1 2 2 2]
[ 3 1 4 3 3]
[ 0 4 2 4 0]
[ 0 0 2 0 0]
[ 0 0 2 0 0]
Wed, 09 Nov 2016 05:27:10 -0600http://ask.sagemath.org/question/35493/how-to-create-a-row-that-displays-the-counts-of-column-entries-in-a-matrix-using-pythonsage/Comment by slelievre for <p>so here is my matrix:</p>
<pre><code>A = matrix([[1,1,2,2,2],
[3,1,4,3,3],
[0,4,2,4,0],
[0,0,2,0,0],
[0,0,2,0,0]])
</code></pre>
<p>I would like to insert a 0 row that displays the amount of row entries in each column.</p>
<p>This was my original code:</p>
<pre><code>A = matrix([[1,1,2,2,2],
[3,1,4,3,3],
[0,4,2,4,0],
[0,0,2,0,0],
[0,0,2,0,0]])
</code></pre>
<p>puzzle = A.insert_row(0, sum(A))
Whats wrong with this code is that it gives me the sum of each column, when I just need the count of each column.</p>
<p>What I have</p>
<pre><code>[ 4 6 12 9 5]
[ 1 1 2 2 2]
[ 3 1 4 3 3]
[ 0 4 2 4 0]
[ 0 0 2 0 0]
[ 0 0 2 0 0]
</code></pre>
<p>Desired</p>
<pre><code>[ 2 3 5 3 2]
[ 1 1 2 2 2]
[ 3 1 4 3 3]
[ 0 4 2 4 0]
[ 0 0 2 0 0]
[ 0 0 2 0 0]
</code></pre>
http://ask.sagemath.org/question/35493/how-to-create-a-row-that-displays-the-counts-of-column-entries-in-a-matrix-using-pythonsage/?comment=35892#post-id-35892See also similar question at [http://stackoverflow.com/questions/40499228/how-to-create-a-row-that-displays-the-counts-of-column-entries-in-a-matrix-using](http://stackoverflow.com/questions/40499228/how-to-create-a-row-that-displays-the-counts-of-column-entries-in-a-matrix-using).Sat, 03 Dec 2016 05:58:09 -0600http://ask.sagemath.org/question/35493/how-to-create-a-row-that-displays-the-counts-of-column-entries-in-a-matrix-using-pythonsage/?comment=35892#post-id-35892Answer by calc314 for <p>so here is my matrix:</p>
<pre><code>A = matrix([[1,1,2,2,2],
[3,1,4,3,3],
[0,4,2,4,0],
[0,0,2,0,0],
[0,0,2,0,0]])
</code></pre>
<p>I would like to insert a 0 row that displays the amount of row entries in each column.</p>
<p>This was my original code:</p>
<pre><code>A = matrix([[1,1,2,2,2],
[3,1,4,3,3],
[0,4,2,4,0],
[0,0,2,0,0],
[0,0,2,0,0]])
</code></pre>
<p>puzzle = A.insert_row(0, sum(A))
Whats wrong with this code is that it gives me the sum of each column, when I just need the count of each column.</p>
<p>What I have</p>
<pre><code>[ 4 6 12 9 5]
[ 1 1 2 2 2]
[ 3 1 4 3 3]
[ 0 4 2 4 0]
[ 0 0 2 0 0]
[ 0 0 2 0 0]
</code></pre>
<p>Desired</p>
<pre><code>[ 2 3 5 3 2]
[ 1 1 2 2 2]
[ 3 1 4 3 3]
[ 0 4 2 4 0]
[ 0 0 2 0 0]
[ 0 0 2 0 0]
</code></pre>
http://ask.sagemath.org/question/35493/how-to-create-a-row-that-displays-the-counts-of-column-entries-in-a-matrix-using-pythonsage/?answer=35496#post-id-35496Here is an option:
r=[Integer(len(A.nonzero_positions_in_column(j))) for j in range(A.dimensions()[1])]
A.insert_row(0, r)
Wed, 09 Nov 2016 15:38:04 -0600http://ask.sagemath.org/question/35493/how-to-create-a-row-that-displays-the-counts-of-column-entries-in-a-matrix-using-pythonsage/?answer=35496#post-id-35496Comment by slelievre for <p>Here is an option:</p>
<pre><code>r=[Integer(len(A.nonzero_positions_in_column(j))) for j in range(A.dimensions()[1])]
A.insert_row(0, r)
</code></pre>
http://ask.sagemath.org/question/35493/how-to-create-a-row-that-displays-the-counts-of-column-entries-in-a-matrix-using-pythonsage/?comment=35502#post-id-35502Nice solution. You could replace `A.dimensions()[1])` by `A.ncols()`.Thu, 10 Nov 2016 03:50:18 -0600http://ask.sagemath.org/question/35493/how-to-create-a-row-that-displays-the-counts-of-column-entries-in-a-matrix-using-pythonsage/?comment=35502#post-id-35502