Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8740473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:08:11+00:00 2026-06-13T11:08:11+00:00

I have an implementation of kalman filter where I am doing matrix operations. At

  • 0

I have an implementation of kalman filter where I am doing matrix operations. At some point I should subtract two 1×1 matrices. I have an error, which I don’t know where it’s coming from.
What is the best way to do matrix operations in python?

import numpy as np
import pylab as pl
import scipy as Sci
import scipy.linalg as linalg

class GetPos(object):
    def __init__(self):
        self.Posp = 0
        self.Velp = 80
        self.z = np.matrix(0)
    def __repr__(self):
        return "from GetPos.__repr__ z=%s" % (self.z)
    def __call__(self):
        self.dt = 0.1
        self.w = 0 + 10*np.random.random()
        self.v = 0 + 10*np.random.random()            
        self.z = self.Posp + self.Velp*self.dt + self.v
        self.Posp = self.z - self.v
        self.Velp = 80 + self.w
        print 'from GetPos.__call__ z = %s' % self.z
        return self.z

class DvKalman(object):
    def __init__(self):
        self.dt = .1
        self.A = np.matrix([[1., self.dt],[0,1]])
        self.H = np.matrix([1., 0])
        self.Q = np.matrix([[1,0.],[0,3]])
        self.R = np.matrix(10)
        self.x = np.matrix([0,20]).T
        self.P = np.matrix(5*np.eye(2))
        #print 'P matrix \n%s' % self.P
        self.firstRun = 0
    def __call__(self, z):
        self.z = z
        print 'from DvKalman.__call__ slef.z = %s and z = %s' % (self.z,z)
        self.xp = self.A * self.x
        self.Pp = self.A*self.P*self.A.T  + self.Q
        self.K = self.Pp * self.H.T * linalg.inv(np.absolute(self.H*self.Pp*self.H.T + self.R));
        print 'from DvKalman.__call__  z=%s, \npreviouse x=\n%s \nH = \n%s \nand P=\n%s \nand xp=\n%s,\n Pp = \n%s,\n K=\n%s' % (self.z,self.x,self.H, self.P,self.xp,self.Pp,self.K)
        newM1 = self.H*self.xp    
        print 'This is self.H*self.xp %s and this is self.z = %s' % (newM1, self.z)
        newM2 = self.z - self.H*self.xp
        print 'This should give simple substruction %s' % newM2                 
        self.x = self.xp + self.K*(self.z - self.H*self.xp)
        self.P = self.Pp - self.K*self.H*self.Pp
        print 'new values x=%s and P=%s' % (self.x,self.P)
        return (self.x)
def TestDvKalman():
    Nsamples = np.arange(0,10,.1)

    kal = DvKalman()
    #print type(kal)
    Xsaved = []
    Zsaved = []

    for i in range(len(Nsamples)):
        z = GetPos()
        print z
        print 'from TestDvKalman zpos = %s' % z
        Zsaved.append(z)
        [position, velocity] = kal(z)
        print position, velocity
        Xsaved.append([position, velocity])
    print Zsaved
    print Xsaved
#    f1 = pl.subplot(121)
#    f1 = pl.plot(Xsaved, 'x-',label = 'Xsaved')
#    f1 = pl.legend()
#    
#    f2 = pl.subplot(122)
#    f2 = pl.title('Kalman Velocity')
#    f2 = pl.plot(Zsaved, 'o-', color = 'brown',label = 'Zsaved')
#    f2 = pl.legend()
#    
#    pl.show()

if __name__ == '__main__':  
    TestDvKalman()

I’ve added a few print lines to follow and debug the code and I added new variable newM which wouldn’t be in the code. The matrices prints correctly This is self.H*self.xp [[ 2.]] and this is self.z = from GetPos.__repr__ z=[[0]] Both matrices are 1×1 but I still recieve an error, don’t know why. The error is:

    newM2 = self.z - self.H*self.xp
TypeError: unsupported operand type(s) for -: 'GetPos' and 'matrix'

I suspect I’m messing up with type somewhere but don’t know where and how to correct it. Can you point me where is the error and how to build a code like that to avoid similar errors in future?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T11:08:12+00:00Added an answer on June 13, 2026 at 11:08 am

    Replace newM2 = self.z - self.H*self.xp by newM2 = self.z() - self.H*self.xp.

    The program should work with that ( but I can’t confirm if it gonna do want you want )

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I am doing some work in C where I have implementation of the
What strategy should I use if I have an implementation of std::fstream with 32-bit
I currently have a implementation where some markers coming from JSON list is shown,
Note:I don't mean some theoretical question which don't have any implementation just languages that
I have special implementation for my grid. For this I wrote some code in
I have an implementation of java.util.Iterator which requires that the call to next() should
I have two implementation of context providers and I know there will be more
I have OnCreateNodePolicy implementation and I'm thinking about adding conditions in binding initialization (some
I have an implementation that just like gmail application, but have met some problem
I downloaded the JSON2.js from https://github.com/douglascrockford/JSON-js/blob/master/json2.js and it does not have implementation for JSON2.stringify()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.