user3407267
user3407267

Reputation:

Matrix Computations Python

I have a function where I am calculating a value as:

a = a + pow(b - np.dot(A[i,:],B[:,j]), 2)    
final_Result= a + C * ((np.square(A)).sum() +(np.square(B)).sum() )
print final_Result
func.extend(final_Result)   
# C is a float value  

It shows me result as

      [[ 455.83301538]]
      >>> func
       [matrix([[ 455.83301538]])]

I can get final.item(0) from the console (IPython). But I couldn't do it in the program. What I am doing wrong here? How can I get the value alone from the final matrix??

Upvotes: 1

Views: 255

Answers (1)

Warren Weckesser
Warren Weckesser

Reputation: 114966

Apparently finalResult is a 1x1 matrix. If you print finalResult, you get the "str" or "nice" representation of finalResult. This format of the matrix doesn't include the text matrix( and the closing ); it just shows the numbers in square brackets. If you enter finalResult in a python prompt, it echoes back the "repr" of finalResult; this include the text matrix(...). You can also get this format using the builtin function repr().

For example (using a instead of finalResult),

>>> import numpy as np
>>> a = np.matrix([[1.25]])
>>> print(a)
[[1.25]]
>>> a
matrix([[1.25]])
>>> print(repr(a))
matrix([[1.25]])

You put finalResult in the list func, which is basically the same as this:

>>> [a]
[matrix([[1.25]])]

To get the value out of the matrix, you can use regular array indexing:

>>> a[0,0]
1.25

Upvotes: 1

Related Questions