Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>A question for your question</h2> <p>While Sven and Navi have answered your question on how to convert </p> <pre><code>x = scipy.matrix([1,2,3]).transpose() </code></pre> <p>into a list, I'll ask a question before answering:</p> <ul> <li>Why are you using an Nx1 matrix instead of an array?</li> </ul> <h2>Using array instead of matrix</h2> <p>If you look at the Numpy for Matlab Users wiki/documentation page, section 3 discusses <a href="http://www.scipy.org/NumPy_for_Matlab_Users#head-e9a492daa18afcd86e84e07cd2824a9b1b651935" rel="nofollow"><em>'array' or 'matrix'? Which should I use?</em></a>. The short answer is that you should use array.</p> <p>One of the advantages of using an array is that:</p> <blockquote> <p>You can treat rank-1 arrays as either row or column vectors. dot(A,v) treats v as a column vector, while dot(v,A) treats v as a row vector. This can save you having to type a lot of transposes.</p> </blockquote> <p>Also, as stated in the <a href="http://docs.scipy.org/doc/numpy/reference/arrays.classes.html#matrix-objects" rel="nofollow">Numpy Reference Documentation</a>, "Matrix objects are always two-dimensional." This is why <code>x.tolist()</code> returned a nested list of <code>[[1], [2], [3]]</code> for you.</p> <p>Since you want an Nx1 object, I'd recommend using array as follows:</p> <pre><code>&gt;&gt;&gt; import scipy &gt;&gt;&gt; x = scipy.array([1,2,3]) &gt;&gt;&gt; x array([1, 2, 3]) &gt;&gt;&gt; y = x.tolist() // That's it. A clean, succinct conversion to a list. &gt;&gt;&gt; y [1, 2, 3] </code></pre> <h2>If you really want to use matrix</h2> <p>If for some reason you truly need/want to use a matrix instead of an array, here's what I would do:</p> <pre><code>&gt;&gt;&gt; import scipy &gt;&gt;&gt; x = scipy.matrix([1,2,3]).transpose() &gt;&gt;&gt; x matrix([[1], [2], [3]]) &gt;&gt;&gt; y = x.T.tolist()[0] &gt;&gt;&gt; y [1, 2, 3] </code></pre> <p>In words, the <code>x.T.tolist()[0]</code> will:</p> <ol> <li>Transpose the x matrix using the <code>.T</code> attribute</li> <li>Convert the transposed matrix into a nested list using <code>.tolist()</code></li> <li>Grab the first element of the nested listed using <code>[0]</code></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload