Changing a matrix element into a scalar
How do I access 1.2 in such a way as to end up with a float? I keep getting a matrix. from numpy import matrix m = matrix([[1.2],[2.3]]) -- Wayne Watson (Watson Adventures, Prop., Nevada City, CA) (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time) Obz Site: 39° 15' 7" N, 121° 2' 32" W, 2700 feet "Republicans are always complaining that government is out of control. If they get into power, they will prove it." -- R. J. Rourke Web Page:<www.speckledwithstars.net/>
Wayne, Matrices are two dimensional arrays so you need two indices to access an individual element: In [1]: from numpy import matrix In [2]: m = matrix([[1.2],[2.3]]) In [3]: m[0,0] Out[3]: 1.2 -paul -----Original Message----- From: numpy-discussion-bounces@scipy.org [mailto:numpy-discussion-bounces@scipy.org] On Behalf Of Wayne Watson Sent: Tuesday, August 03, 2010 9:24 AM To: Discussion of Numerical Python Subject: [Numpy-discussion] Changing a matrix element into a scalar How do I access 1.2 in such a way as to end up with a float? I keep getting a matrix. from numpy import matrix m = matrix([[1.2],[2.3]]) -- Wayne Watson (Watson Adventures, Prop., Nevada City, CA) (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time) Obz Site: 39° 15' 7" N, 121° 2' 32" W, 2700 feet "Republicans are always complaining that government is out of control. If they get into power, they will prove it." -- R. J. Rourke Web Page:<www.speckledwithstars.net/> _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Thank you. That's almost amusing. Too logical for Python. I fully expected something like m{0}[0](0)[0:0]. :-) I don't think the tentative Numpy tutorial mentions it. On 8/3/2010 9:28 AM, PHobson@Geosyntec.com wrote:
Wayne,
Matrices are two dimensional arrays so you need two indices to access an individual element:
In [1]: from numpy import matrix
In [2]: m = matrix([[1.2],[2.3]])
In [3]: m[0,0] Out[3]: 1.2
-paul
-----Original Message----- From: numpy-discussion-bounces@scipy.org [mailto:numpy-discussion-bounces@scipy.org] On Behalf Of Wayne Watson Sent: Tuesday, August 03, 2010 9:24 AM To: Discussion of Numerical Python Subject: [Numpy-discussion] Changing a matrix element into a scalar
How do I access 1.2 in such a way as to end up with a float? I keep getting a matrix. from numpy import matrix m = matrix([[1.2],[2.3]])
-- Wayne Watson (Watson Adventures, Prop., Nevada City, CA) (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time) Obz Site: 39° 15' 7" N, 121° 2' 32" W, 2700 feet "Republicans are always complaining that government is out of control. If they get into power, they will prove it." -- R. J. Rourke Web Page:<www.speckledwithstars.net/>
I don't think I've ever actually seen someone use the matrix datatype instead of the array datatype. Hopefully nobody minds me asking the noob question: What's the advantage of the matrix datatype? --Josh On Tue, Aug 3, 2010 at 8:59 AM, Wayne Watson <sierra_mtnview@sbcglobal.net> wrote:
Thank you. That's almost amusing. Too logical for Python. I fully expected something like m{0}[0](0)[0:0]. :-)
I don't think the tentative Numpy tutorial mentions it.
On 8/3/2010 9:28 AM, PHobson@Geosyntec.com wrote:
Wayne,
Matrices are two dimensional arrays so you need two indices to access an individual element:
In [1]: from numpy import matrix
In [2]: m = matrix([[1.2],[2.3]])
In [3]: m[0,0] Out[3]: 1.2
-paul
-----Original Message----- From: numpy-discussion-bounces@scipy.org [mailto:numpy-discussion-bounces@scipy.org] On Behalf Of Wayne Watson Sent: Tuesday, August 03, 2010 9:24 AM To: Discussion of Numerical Python Subject: [Numpy-discussion] Changing a matrix element into a scalar
How do I access 1.2 in such a way as to end up with a float? I keep getting a matrix. from numpy import matrix m = matrix([[1.2],[2.3]])
-- Wayne Watson (Watson Adventures, Prop., Nevada City, CA)
(121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time) Obz Site: 39° 15' 7" N, 121° 2' 32" W, 2700 feet
"Republicans are always complaining that government is out of control. If they get into power, they will prove it." -- R. J. Rourke
Web Page:<www.speckledwithstars.net/>
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
On 8/3/2010 1:29 PM, Joshua Holbrook wrote:
What's the advantage of the matrix datatype?
As it turns out, that's a controversial question. ;-) One answer: pedagogy (for those used to matrices). A related answer: succinctness and readability of *some* code. >>> a,b,c = np.array([[1,2,3]]), np.array([[4],[5],[6]]), np.arange(9).reshape((3,3)) >>> d = np.dot(b,np.dot(a,c)) # matrix multiplication >>> d2 = b.dot(a.dot(c)) # NumPy 1.5+ >>> d5 = d.dot(d).dot(d).dot(d).dot(d) #matrix exponentiation >>> d[0,0] = 0 >>> di = np.linalg.inv(d) #matix inverse >>> A,B,C = np.mat('1 2 3'), np.mat('4;5;6'), np.mat(np.arange(9).reshape((3,3))) >>> D = B * (A * C) #matrix multiplication >>> D5 = D**5 #matrix exponentiation >>> D[0,0] = 0 >>> DI = D.I #matix inverse fwiw, Alan Isaac
On Tue, Aug 3, 2010 at 10:44 AM, Alan G Isaac <alan.isaac@gmail.com> wrote:
On 8/3/2010 1:29 PM, Joshua Holbrook wrote:
What's the advantage of the matrix datatype?
As it turns out, that's a controversial question. ;-)
One answer: pedagogy (for those used to matrices).
A related answer: succinctness and readability of *some* code.
>>> a,b,c = np.array([[1,2,3]]), np.array([[4],[5],[6]]), np.arange(9).reshape((3,3)) >>> d = np.dot(b,np.dot(a,c)) # matrix multiplication >>> d2 = b.dot(a.dot(c)) # NumPy 1.5+ >>> d5 = d.dot(d).dot(d).dot(d).dot(d) #matrix exponentiation >>> d[0,0] = 0 >>> di = np.linalg.inv(d) #matix inverse >>> A,B,C = np.mat('1 2 3'), np.mat('4;5;6'), np.mat(np.arange(9).reshape((3,3))) >>> D = B * (A * C) #matrix multiplication >>> D5 = D**5 #matrix exponentiation >>> D[0,0] = 0 >>> DI = D.I #matix inverse
fwiw, Alan Isaac
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
I see. Thanks Alan! --Josh
Joshua Holbrook wrote:
I don't think I've ever actually seen someone use the matrix datatype instead of the array datatype.
Hopefully nobody minds me asking the noob question: What's the advantage of the matrix datatype?
The advantage of the matrix datatype is that it is a matrix in the linear algebra sense. i.e. A*B is matrix multiplication. As a matrix is, by definition, a 2-d array, matrix operations return 2-d arrays -- hence the OP's issue. I think many of us find n-d arrays far more powerful and useful, and the extra overhead of code for the handful of linear algebra operations in a given piece of code is well worth it. For those that really want a natural way to express linear algebra, the matrix class really needs some more work to do the job well. (see wiki and discussion on this list). However, no one has stepped up to do the hard work of making any of those suggestions a reality, so it's not been done. -Chris
--Josh
On Tue, Aug 3, 2010 at 8:59 AM, Wayne Watson <sierra_mtnview@sbcglobal.net> wrote:
Thank you. That's almost amusing. Too logical for Python. I fully expected something like m{0}[0](0)[0:0]. :-)
I don't think the tentative Numpy tutorial mentions it.
On 8/3/2010 9:28 AM, PHobson@Geosyntec.com wrote:
Wayne,
Matrices are two dimensional arrays so you need two indices to access an individual element:
In [1]: from numpy import matrix
In [2]: m = matrix([[1.2],[2.3]])
In [3]: m[0,0] Out[3]: 1.2
-paul
-----Original Message----- From: numpy-discussion-bounces@scipy.org [mailto:numpy-discussion-bounces@scipy.org] On Behalf Of Wayne Watson Sent: Tuesday, August 03, 2010 9:24 AM To: Discussion of Numerical Python Subject: [Numpy-discussion] Changing a matrix element into a scalar
How do I access 1.2 in such a way as to end up with a float? I keep getting a matrix. from numpy import matrix m = matrix([[1.2],[2.3]])
-- Wayne Watson (Watson Adventures, Prop., Nevada City, CA)
(121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time) Obz Site: 39° 15' 7" N, 121° 2' 32" W, 2700 feet
"Republicans are always complaining that government is out of control. If they get into power, they will prove it." -- R. J. Rourke
Web Page:<www.speckledwithstars.net/>
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
-- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chris.Barker@noaa.gov
On 8/3/2010 12:23 PM, Wayne Watson wrote:
How do I access 1.2 in such a way as to end up with a float? I keep getting a matrix. from numpy import matrix m = matrix([[1.2],[2.3]])
Matrices have the odd (and imo undesirable) property that m[0,0] != m[0][0] You want the former. For a discussion that eventually aborted, see http://www.scipy.org/MatrixIndexing Alan Isaac
participants (6)
-
Alan G Isaac -
Christopher Barker -
Joshua Holbrook -
PHobson@Geosyntec.com -
Skipper Seabold -
Wayne Watson